-1
for (var i = 1; i < 100; i++) { 
if(document.FORM.chkTableType[i].checked==false && 
    document.FORM.chkTableType[i]+"_"+"1".checked==false &&
    document.FORM.chkTableType[i]+"_"+"2".checked==false )
    {
        window.alert("Please select at least 1 table to download");
        return false;
    }
}

I want to validate combo box in javascript, the purpose if having this document.FORM.chkTableType[i]+"_"+"1" is to generate something like the following:

document.FORM.chkTableType1_1

document.FORM.chkTableType1_2

document.FORM.chkTableType2_1

document.FORM.chkTableType2_1

but it throws error: Unable to get property '1' of undefined or null reference

im not sure where whether the syntax of this is correct---> document.FORM.chkTableType[i]+"_"+"1"

2
  • yep, this is incorrect syntax document.FORM.chkTableType[i]+"_"+"1".checked Commented Nov 25, 2014 at 11:41
  • may i ask what should be the correct syntax? tried to google around but no luck. Commented Nov 25, 2014 at 11:44

1 Answer 1

1

If I understand correctly, you're trying to build the property name dynamically like this.

for (var i = 1; i < 100; i++) { 
if(document.FORM['chkTableType' + i].checked==false && 
    document.FORM['chkTableType' + i + '_1'].checked==false &&
    document.FORM['chkTableType' + i + '_2'].checked==false )
    {
        window.alert("Please select at least 1 table to download");
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Incidentally, it might be a little cleaner to find the form using document.getElementById and use HTMLFormElement.elements to find the inputs. - developer.mozilla.org/en-US/docs/Web/API/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.