473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Getting reference to element with array-like attribute

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
Aug 13 '08 #1
5 1317
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

"laredotornado" <la***********@zipmail.comwrote in message
news:4d**********************************@w39g2000 prb.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

Aug 13 '08 #2
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 ofthe
inputs:

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

Tim

"laredotornado" <laredotorn...@zipmail.comwrote in message

news:4d**********************************@w39g2000 prb.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?

Thanks, - Dave
Aug 13 '08 #3

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
*Literally* with those id's and names, or are you using
"prescription_number" as a placeholder ? If they're literally like that then
you should consider a different naming strategy.

Your example:

document.forms[0].elements[2]
or more generally:
document.forms[0].elements[document.forms[0].elements.length-1]

....assuming that's the first form on the page and there are no other inputs
you're not showing.

Tim


Aug 14 '08 #4
On Aug 14, 1:15*am, "laredotorn...@zipmail.com"
<laredotorn...@gmail.comwrote:
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
"laredotornado" <laredotorn...@zipmail.comwrote in message
news:4d**********************************@w39g2000 prb.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.
--
Rob
Aug 14 '08 #5
On Aug 13, 11:56*pm, RobG <rg...@iinet.net.auwrote:
On Aug 14, 1:15*am, "laredotorn...@zipmail.com"

<laredotorn...@gmail.comwrote:
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
"laredotornado" <laredotorn...@zipmail.comwrote in message
>news:4d**********************************@w39g200 0prb.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.

--
Rob- Hide quoted text -

- Show quoted text -
Thanks, Rob for your detailed suggestions. I ended up going with the
first one. - Dave
Aug 14 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: JKop | last post by:
template<class T> inline T& NthArrayMember( T & (array),size_t i) { return array; } template<class T> inline const T& NthArrayMember( const T & (array),size_t i) { return array;
1
by: George W. | last post by:
Okay, I'm a C#/XML newbie, and I've been wrestling with this for a while now, checked dotnet sites, articles, MSDN Library, etc. and haven't been able to determine why this is happening. I have...
7
by: Jeff K | last post by:
Can you pass an int array by reference to a function and modify selective elements? Here is my code: #include <stdio.h> #define COLUMNSIZE 30 #define ASIZE 5...
5
by: Tom Jones | last post by:
Hi, Say I have a managed array: int myArray = { 1, 2, 3, 4, 5 }; How can I define a reference to say the 3rd element of that array? I should be able to use this reference to obtain the value...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
7
by: rossum | last post by:
Is there any way of creating a read only reference to an array? My class includes a private array of bytes with a Property to access it. However, I do not want users to use the returned reference...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
10
by: garyusenet | last post by:
Hi I'm using the following to reference each 'element' (is this the correct term) in an arraylist: - foreach (InternetExplorer ie in ar) { ... } Q. How do I reference the nth element in an...
5
by: dwmartin18 | last post by:
Hello everyone. I have quite the puzzling problem with a script I have been working on lately. I have created a function that can be called to create a new html element (e.g. input, select, div,...
9
by: grocery_stocker | last post by:
How are references and aliases in perl different than references in aliases in C++?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.