On Aug 13, 1:21 am, "Tim Williams" <timjwilliams at gmail dot com>
wrote:
Your question is unclear. If you meant how do you get access to one of the
inputs:
document.getElementById("form_items[][prescription_number]")
or
document.yourFormName.elements["form_items[][prescription_number]"]
Tim
Hi,
I have a number of INPUTs on my page that look like
<input id="form_items[][prescription_number]" name="form_items[]
[prescription_number]" size="15" type="text" value="..." />
How do I refer the last one of these on my page?
Thanks, - Dave- Hide quoted text -
- Show quoted text -
Sorry for the confusion. What I meant to ask is, say I have a form
like this:
<form>
<input id="form_items[][prescription_number]" name="form_items[]
[prescription_number]" size="15" type="text" value="..." />
<input id="form_items[][prescription_number]" name="form_items[]
[prescription_number]" size="15" type="text" value="..." />
<input id="form_items[][prescription_number]" name="form_items[]
[prescription_number]" size="15" type="text" value="..." />
</form>
How do I get a reference to the 3rd input field?
For this specific example:
var name = 'form_items[][prescription_number]';
document.getElementsByName(name)[2];
You could also do something like:
var form = document.forms[0];
var name = 'form_items[][prescription_number]';
form.elements[name][2];
or more generally, if you want the last one:
var form = document.forms[0];
var name = 'form_items[][prescription_number]';
var inputs = form && form.elements[name];
if (inputs && inputs.length > 0) {
var lastInput = inputs[inputs.length - 1];
}
Rather than relying on the form index, it would be better give your
form a name or id and access it using:
var form = document.forms[name];
or
var form = document.getElementById(id);
respectively.