views:

109

answers:

2

I have this HTML code:

<form id="form1" name="form1" method="post" action="">
a <input type="text" name="item[]" value="1" /> <br />
b <input type="text" name="item[]" value="1" /> <br />
c <input type="text" name="item[]" value="1" />
</form>

I can't seem to pro grammatically pull the length out of the text fields array , is there some obvious plain JavaScript way (no Jquery please) to do this? I tried

    <script language="javascript">
//Tried these combination
alert(document.form1.item.length);
alert(document.form1.item[].length);
alert(document.form1.elements['item'].length);
alert(document.form1.elements['item[]'].length);
</script>
+2  A: 
var inputCount = document.getElementById('form1').getElementsByTagName('input').length;

Note that, strictly speaking, your input elements do not constitute an array. They're nodes in the DOM, so you have to find them as an array.

I know you said "no jQuery", but in this day and age it really perplexes me when people are resistant to using tools like that. They solve lots of problems that you'll otherwise end up solving yourself.

Pointy
Thanks but I should mention this is for MOBILE IE which I don't think has getElementById support...
I would be shocked to learn that it has no getElementById() support. Now, it's probably broken like it is in regular IE (it returns elements based on the "name" attribute sometimes), but it would be pretty weird for it to be missing completely. Then again, assuming Microsoft wouldn't do something weird like that is probably a bad idea.
Pointy
Thanks will confirm myself.
http://channel9.msdn.com/wiki/MobileDeveloper/GetElementByID/ covers that.
Matt Ball
:headslap: I don't think my teeth could stand the grinding if I were an MS Mobile developer - you have my admiration.
Pointy
A: 
document.getElementById('form1').childElementCount

should do it.

Edit: in light of this being specific to Windows Mobile, Channel 9 covers what you're looking for. The availability of getElementById depends on whether or not your target has "Messaging and Security Feature Pack (AKU2)."

Matt Ball