Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2009, 12:01 AM   PM User | #1
Ezequal
New to the CF scene

 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Ezequal is an unknown quantity at this point
Unhappy getElementsByName() question

After some severe experimentation I got to googling for an answer and stumbled upon these forums.

So my big question is, is it possible to use getElementsByName() with a variable as the parameter? And if not what is another way I can get at each element based on a variable list of names.

Code:
var curObjSet = document.getElementsByName(String(idStack[y])).item(0)
I'm trying desperately to make my code organic and not to have to hard code field names but without this working its leaving me with out options.

Thank you for any time and help provided.

- Eric
Ezequal is offline   Reply With Quote
Old 10-01-2009, 12:33 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
depending on what's in idStack[y], it should be fine.

i don't remember .item(0), perhaps [0] would work better.

also, it's probably better to use ID since some browsers are screwy with the name property.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 10-01-2009, 12:48 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Iyou use ID then getElementsByName() won't work if the name and ID aren't the same.

And I dunno how getElementsByName() behaves on a set of radio buttons. Is it the same in all browsers??

Ahhh...w3schools says that indeed you get one reference to the entire set, with the set converted to an array:
http://www.w3schools.com/htmldom/met...entsbyname.asp

Okay, consistent, at least.
Old Pedant is offline   Reply With Quote
Old 10-01-2009, 03:20 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Old Pedant View Post
Iyou use ID then getElementsByName() won't work if the name and ID aren't the same.

And I dunno how getElementsByName() behaves on a set of radio buttons. Is it the same in all browsers??
.
input names are probably pretty cohesive between browsers. but name's behavior on non-form elements is inconstant, notably so in IE.

if you need to grab a few non-form elements, use document.getElementById in conjunction with unique ids.

if you need several, or a couple groups of non-form elements, use class attribs and the ubiquitous getElementsByClass, available in the "post a javascript" section.


if you just want to grab groups of inputs, i apologize for any confusion, getElementsByName should work just fine for that.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Tags
dynamic, getelementsbyname, variable

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:47 PM.


Advertisement
Log in to turn off these ads.