Solved

select.options as associative array

Posted on 2002-05-15
8
281 Views
Last Modified: 2009-12-16
I want to access the select.options collection as an associative array, but can't seem to get it to work, e.g.

<select>
<option id="option1" value="nar">blah
</select>
<script>
refToSelect.options["option1"].value= "hello";
</script>

As just about everything in JavaScript is an associative array, I thought this would work, but it doesn't.

Any ideas on how to make it work (and don't tell me to loop through the options and match - thats what I want to avoid).

Leon
0
Comment
Question by:Astroman
  • 4
  • 4
8 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 7010997
document.getElementById("option1").value
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7011005
<script>
document.getElementById("option1").value = "hello";
</script>
0
 
LVL 1

Author Comment

by:Astroman
ID: 7011151
I know that strictly that works, but I'd prefer (from a code readability point of view) to reference it via the options, or at least the select object, so it's clear what the code is doing.

I guess I could do it using the children collection of select?

Cheers,

Leon
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 7011179
yes, perhaps:

refToSelect.children.option1.value = "hello";
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:Astroman
ID: 7011185
I'll leave this a bit longer, but I guess this is the best way. If so, I'll give you the points.  Thanks kek.

0
 
LVL 1

Author Comment

by:Astroman
ID: 7011254
Dunno what to do now. Turns out my original post was correct, but my (SS generated) code had the element id as a number, which is wrong and was breaking it.

So the answer is, 'options' is an associative array (as I suspected), and I'm silly.

I'll give you the points, because someone should have them, and at least it got me debuggin it properly....
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7011265
I see, I thought that it should have worked as you originally described it, but I just assumed not since you had tried it.  Oh well, thanks for the points, and for the exercise.
0
 
LVL 1

Author Comment

by:Astroman
ID: 7011295
actually, I'm beginning to feel a complete fool. Having ( I thought) verified that it did actually work, it seems to have stopped working now.  I dunno. type first, think later today.

'children' works for sure, anyway, so I'm happy (ish)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now