• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript elementName

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have a problem with JavaScript.
I can use document.getElementById('bleh') to get an element
I can also use document.getElementById('bleh').value to get the value.
But what if I want the tag to contain the attribute name as well.
My question is, how can I get the value of the name attribute using document.getElementById.

i.e:

Assuming a select box

<select>
<option id='yo' value='yes' name='ss'/>
<option id='yo2' value='33' name=24'/>
</select>

Is it possible to get the name attribute of either the yo or the yo2 option boxes??
Thank you.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
name is not a valid HTML attribute of the <option> tag.

That said, document.getElementById('yo').name seems to work under IE. It does not work under any other browser that I tried (Safari, Firefox, et al).

What are you really trying to accomplish?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the SELECT in various ways. From that you can get at its OPTIONS. It should then be possible to call getAttribute("name") on an individual OPTION.
 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi figured it out:

//Using the id of the <SELECT> tag , here it is Clients, get the currentindex
var selectedNumber = document.getElementById('Clients').selectedIndex;

//theOptions will return the select tag itself
var theOptions = document.getElementById('Clients');

//Then use to get the name attribute.
var theMenuCode = theOptions [selectedNumber].getAttribute("name");

Thanks everyone.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic