473,396 Members | 1,693 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,396 software developers and data experts.

Accessing elements of a form...

We can access the Nth element in the first form by

document.forms[0].elements[N]

imagine that theres function like
<INPUT TYPE=TEXT" onclick="somefunction(this)">
and...
we have

function somefunction(obj) {
how can we obtain the N from within the function???
Jul 20 '05 #1
4 1367
On Sat, 6 Mar 2004 12:41:31 +0100, Paweł <pm**@op.pl> wrote:
We can access the Nth element in the first form by

document.forms[0].elements[N]

imagine that theres function like
<INPUT TYPE=TEXT" onclick="somefunction(this)">
I assume that's a typo, and you meant: ... type="text" ... Note the
difference in quotes.
and...
we have

function somefunction(obj) {

how can we obtain the N from within the function???


obj
contains a reference to that INPUT element.
obj.form
will produce a reference to the INPUT element's containing form.
obj.form.elements[ N ]
will produce a reference to the Nth element of the INPUT element's
containing form.

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
On Sat, 06 Mar 2004 12:37:29 GMT, in comp.lang.javascript you wrote:
On Sat, 6 Mar 2004 12:41:31 +0100, Paweł <pm**@op.pl> wrote:
We can access the Nth element in the first form by

document.forms[0].elements[N]

imagine that theres function like
<INPUT TYPE=TEXT" onclick="somefunction(this)">


I assume that's a typo, and you meant: ... type="text" ... Note the
difference in quotes.
and...
we have

function somefunction(obj) {

how can we obtain the N from within the function???


obj
contains a reference to that INPUT element.
obj.form
will produce a reference to the INPUT element's containing form.
obj.form.elements[ N ]
will produce a reference to the Nth element of the INPUT element's
containing form.

Hope that helps,

Well it doesn't ...
What I ask about is how do we know that Nth element is Nth??
Let's say we have a row in the table, abd for each row we have rowIndex...
I would like to have similiar property for Input element...
Any ideas?

Jul 20 '05 #3
On Sat, 6 Mar 2004 20:16:18 +0100, Paweł <pm**@op.pl> wrote:

[snip]
What I ask about is how do we know that Nth element is Nth??
Let's say we have a row in the table, abd for each row we have
rowIndex...
I would like to have similiar property for Input element...


You mean you want to know the index of a particular form control when you
have a reference (and only a reference) to that control?

There's no direct method. You'll have to loop through all controls in the
form and check if the "mystery" reference matches a reference in the
elements collection:

function getIndex( obj ) {
var e = obj.form.elements, n = e.length;

for( var i = 0; i < n; ++i )
if( obj == e[ i ]) return i;
return null;
}
...
<form ...>
<input type="button" onclick="alert(getIndex(this))">
<input type="button" onclick="alert(getIndex(this))">
</form>

Clicking on the first button will display 0. Clicking the second will
display 1.

Is that better?

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
function getIndex( obj ) {
var e = obj.form.elements, n = e.length;

for( var i = 0; i < n; ++i )
if( obj == e[ i ]) return i;
return null;
}
Is that better?


Yeah, that's obviously better. Thank you, currently I'm working on a script
to validate a form, and I would like my script to focus back on input field
in case user enters an incorrect value. So now, I can just call the focus
method on (N-1)th element and that should be fine I hope ;)
Jul 20 '05 #5

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

Similar topics

1
by: DU | last post by:
Assuming you have a typical form built in this manner: <form action="..."> <p><input id="idInputText" name="nameInputText" type="text" size="20"></p> <p><input id="idCheckbox"...
4
by: f_salazar | last post by:
English Version !! Hi !! I have a page with a Form, inside the form I have listbox with 'X' elements. Wend I submit the page, an ASP process inside the page have to read those elements and...
5
by: Craig Anderson | last post by:
Can anyone tell me the best way to access a hidden object in a form? I could use a hard-coded index to the elements of the form, but it's too easy to add something before the hidden object and mess...
5
by: Rune Runnestø | last post by:
How do I focus the cursor in the input field 'numberField' when accessing this jsp-file (or html-file) ? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head>...
18
by: Choxio | last post by:
Using ASP you can access posted form elements. Is it possible to access them from JavaScript? myHTML.htm <form action=myASP.asp> <input type=hidden id=f1 value=val1> <input type=hidden...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
1
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
6
by: Chuck Anderson | last post by:
My knowledge of JavaScript is limited. I learn from example and then adapt those examples to suit my needs. I have stumped myself on this one. I have a form with checkboxes that I want to...
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.