Skip to content Skip to sidebar Skip to footer

Getting Html Array Length With Javascript

I am dynamically creating inputs when the users double clicks an element in a select box. There can be multiple inputs per element type, so I am creating them as HTML arrays. When

Solution 1:

You can use the following pure JavaScript code:

oForm = document.forms[0];
oForm.elements['start_' + field + '[]'].length;

Or in jQuery library:

$('input[name="start_' + field + '[]"]').length;

I would prefer the second because I am not 100% sure about cross-browser compatibility of the first solution.

Solution 2:

what about using this syntax ?

onclick="myFunction( event, this )"

using this you can pass any important data - mouseposition in first parameter and object in second

in your case the syntax is invalid, during to the fact id must be unique, and foooBar[] is same id as another foooBar[] ( in fact HTML doesn't know nothing about arrays )

Solution 3:

document.getElementById doesn't return a HTML Collection, so it does not have a length property.

Post a Comment for "Getting Html Array Length With Javascript"