Getting reference to element with array-like attribute

L

laredotornado

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
 
L

laredotornado

Your question is unclear.  If you meant how do you get access to one ofthe
inputs:

document.getElementById("form_items[][prescription_number]")
or
document.yourFormName.elements["form_items[][prescription_number]"]

Tim




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?

Thanks, - Dave
 
R

RobG

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]"]

news:4d1c144b-9da3-4e23-81e6-5d8db957342f@w39g2000prb.googlegroups.com....
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.
 
L

laredotornado

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.

Thanks, Rob for your detailed suggestions. I ended up going with the
first one. - Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,360
Latest member
kathdev

Latest Threads

Top