0

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 the inputs are created, a link is also created that points to a javascript function. Said function will open a calendar prompt for the specific field that was clicked. I have narrowed down the issue to the below code:

document.getElementById( 'start_' + field + '[]' ).length

As you see, I am trying to grab the length of the array just like I would with a normal javascript array. This, of course, is not working. I am unable to find anything on Google referencing this, which usually means it is impossible but I find that hard to believe.

Alternatively, I can increment a variable every time this field is created I just wanted to make sure I am not missing something painfully obvious.

Thank you in advance!

{EDIT}

Please reference http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs before telling me that HTML does not allow arrays.

{DoubleEdit+Answer}

The issue was not that HTML arrays do not exist, but that the ID attribute cannot be an array. I found http://roshanbh.com.np/2008/08/handling-array-html-form-elements-javascript-php.html which explains how to do what I need...ish.

1
  • 2
    HTML doesn't have arrays, you are getting confused by PHP's form data parsing rules. You can't have multiple elements with the same id, and you can't use the caracters [ or ] in an HTML 4,x or XHTML ID. Commented Aug 29, 2011 at 8:46

3 Answers 3

2

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.

0
1

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 )

4
  • This is already in the onClick function. The problem is that the element being clicked is a select box. I can parse all necessary information from this element, then I create the input box. I then need to get the length of the input box HTML array so that I can create the link to reference that specific index in the array Commented Aug 29, 2011 at 8:44
  • You are incorrect, HTML does allow arrays and I use them all the time. It dynamically expands the array as needed, I just don't know how to access said array. If I pass the array through to PHP on the backend, I receive an array such as $_POST['input'][0], etc. The question is can javascript also access that array? Commented Aug 29, 2011 at 8:46
  • you are wrong, this is NOT feauture of HTML, but PHP - and it is applied to name, not id - so the answer is - no , javascript cannot, because this is not parsed as array ( this is done by PHP on server ) - you can get array from document.getElementsByTagName( '*' ); ( or replace document with parent node ) Commented Aug 29, 2011 at 8:47
  • you got me to the right place. HTML arrays can be used in the name attribute but not in the id attribute. Thank you so much, I have accepted the answer Commented Aug 29, 2011 at 8:50
0

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

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.