hades
hades

Reputation: 4696

javascript not sure about the for loop syntax

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"

Upvotes: 0

Views: 42

Answers (1)

Sam Greenhalgh
Sam Greenhalgh

Reputation: 6136

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;
    }
}

Upvotes: 1

Related Questions