I am trying to figure out a way to somehow obtain the index number of a form element by using the form element's name property. Can anyone suggest a way to go about this?
What I am doing is this:
I have a dynamically generated form that has a variable number of elements. If a user changes something within one of the form elements (an onChange event handler), the form re-structures itself, and quite possibly adds a variable number of elements to itself.
I have figured out how to grab the name property of the form element that changed (through the onChange event handler), but I cannot figure out how to grab that form element's index number.
Why I want the index number:
When the page reloads with the new form elements, I want to focus the cursor in the next form element. In other words, let's say I have the following code (saved as testing.php):
Code:
<?php extract($_POST); ?>
<form name="my_form" action="./testing.php" method="POST">
<input type="text" name="textbox1" />
<br />
<select name="select1" onChange="document.my_form.submit();">
<option<?php if(empty($select1)) { echo ' selected="selected"'; } ?>>-- Pick One --</option>
<option value="hello"<?php if($select1 == "hello") { echo ' selected="selected"'; } ?>>Hello</option>
<option value="goodbye"<?php if($select1 == "goodbye") { echo ' selected="selected"'; } ?>>Goodbye</option>
</select>
<br />
<?php
if($select1 == "hello") {
echo '<input type="text" name="hello_text1"';
if(isset($hello_text1)) { echo " value=\"$hello_text1\""; }
echo ' />
<br />';
}
?>
<select name="select2" onChange="document.my_form.submit();">
<option<?php if(empty($select2)) { echo ' selected="selected"'; } ?>>-- Pick One --</option>
<option value="test1"<?php if($select2 == "test1") { echo ' selected="selected"'; } ?>>Testing Once</option>
<option value="test2"<?php if($select2 == "test2") { echo ' selected="selected"'; } ?>>Testing Again</option>
</select>
<br />
<?php
if($select2 == "test1") {
echo '<input type="text" name="test1_text"';
if(isset($test1_text)) { echo " value=\"$test1_text\""; }
echo ' />
<br />';
}
elseif ($select2 == "test2") {
echo '<select name="test2_select1">
<option';
if(empty($test2_select1)) { echo ' selected="selected"'; }
echo '>-- Pick One --</option>
<option value="test2_option1"';
if($test2_select1 == "test2_option1") { echo ' selected="selected"'; }
echo '>Testing Again Option 1</option>
<option value="test2_option2"';
if($test2_select1 == "test2_option2") { echo ' selected="selected"'; }
echo '>Testing Again Option 2</option>
</select>
<br />';
}
?>
<input type="submit" value="Submit This Form" />
</form>
Obviously, when you first load the page, the form will only have four elements (including the submit button). However, as you change the values of specific elements, the total number of form elements will change, meaning that the index number of those form elements will change.
If I want to advance the focus to the next element in the form after the page reloads, I am assuming that I will need to find the index number of the form element that the user changed before the page reloaded, then add 1 to that index number, and place the focus there.
So, as I said, here is my question:
Using javascript, if I have the name of a form element, can I somehow obtain the index number of that form element?