Using javascript how do I get the number of elements present with the id of id[x]?
Sample HTML:
<input name="vrow[0]" id="vrow[0]" value="0" type="text"/>
<input name="vrow[1]" id="vrow[1]" value="0" type="text"/>
<input name="vrow[2]" id="vrow[2]" value="0" type="text"/>
<input name="vrow[3]" id="vrow[3]" value="0" type="text"/>
The above html is generated depending on user input. How do I detect how many elements are present using javascript?
Currently I can detect presence of an element like this
Sample Javascript
if(document.getElementById('vrow[0]')){
var row=0;
}
if(document.getElementById('vrow[1]')){
row=1;
}
...
[]are not valid characters in ID attributes in HTML4.01. Even in HTML5, however, you should use the name attribute (without the numeric indexes), and then use getElementsByName():Note that older versions of IE and Opera may return elements with
idattributes that have the same value as the name specified in getElementsByName(). IE may also return non-input elements with a name attribute that has the same value.