I have an entry form that is dynamic (any number of items can be added.) The user clicks a button to add another entry. The html form field is an array. It works perfectly except I can not figure out how to change the focus through javascript.
My question is, what would be a way to change focus on an input that has is being built dynamically and has the same name? I want the latest created field to have the focus.
There is such a thing as an array on an html form... simply use <input type='text' value='Apples' name='fruit[]'> and <input type='text' value='Oranges' name='fruit[]'> and on the server side you can retrieve the value of fruit as an array. How would you set the focus on the second field (Oranges) using focus()?
I don't know where to begin on traversing multiple elements with the same name.
The jQuery does look trivial - but keep in mind that the value would be blank... so I assume I would tell it to focus on a value that is blank, which doesn't sound like a good idea.
if you know it's the last one. (In other words, there's myriad ways to slice and dice with jQuery to pick particular elements without resorting to traversal).
In raw JS, look into getElementsByTagName() and start iterating!
Getting very close... I am using a counter and reference it with:
document.inputform.elements["documents[]"][counter].focus();
Focus works the first time it adds a field in IE8, but when you add a third, forth, etc. field the focus doesn't change (just goes away). I don't get a JavaScript error.