JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 24th, 2010, 04:06 AM
doctormelodious's Avatar
doctormelodious doctormelodious is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 122 doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 9 h 9 m 2 sec
Reputation Power: 64
Does a form element know its own index number in the forms[].elements[] array?

I have a form element that looks like this:

<input type="text" onchange=doSomething(this)>

and a function:

function doSomething(theField){
}

I know that within the function I can access properties of the field (e.g. theField.name and theField.value). But how do I access theField's index number in the form's elements[] array -- from the "this" reference that was passed to the function? I.E. if this field is elements[3], how can I get at that 3?

Thanks!
DM

Reply With Quote
  #2  
Old August 24th, 2010, 08:09 PM
Arty Effem's Avatar
Arty Effem Arty Effem is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Location: England
Posts: 398 Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arty Effem User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 8 h 25 m 49 sec
Reputation Power: 71
I don't know of such a property but if you need to know it:
Code:
function doSomething(theField)
{
 var elems = theField.form.elements, len = elems.length;

 for( var i = 0; i < len && elems[ i ] !== theField; i++ )
 ;

 /* i holds the element's index */
}
__________________


No it's not 'awesome' - it's just code.

Reply With Quote
  #3  
Old August 27th, 2010, 07:30 PM
doctormelodious's Avatar
doctormelodious doctormelodious is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 122 doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level)doctormelodious User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 9 h 9 m 2 sec
Reputation Power: 64
Thanks Arty. That's actually pretty close to what I ended up doing.


Quote:
Originally Posted by Arty Effem
I don't know of such a property but if you need to know it:
Code:
function doSomething(theField)
{
 var elems = theField.form.elements, len = elems.length;

 for( var i = 0; i < len && elems[ i ] !== theField; i++ )
 ;

 /* i holds the element's index */
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Does a form element know its own index number in the forms[].elements[] array?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap