[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Select corresponding select option by value

Posted on 2006-10-19
5
Medium Priority
?
1,620 Views
Last Modified: 2008-02-01
Hi,

I have a select object and the following script
document.getElementsByName(objSel).length
is giving me 1

I have four options in it
0
Comment
Question by:JoeUS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:netsmithcentral
ID: 17770328
document.getElementsByName(name) returns an array containing object references to every object with that name on a page.  To access the acutal object you have to refer to it's number within the array:

document.getElementsByName(objSel)[0].length; <== will return the length of the select.

A better method is to use the document.getElementById(id) method.  This returns exactly one object, the one with a given ID.
0
 

Author Comment

by:JoeUS
ID: 17770810
In this case I am looking for the lenght of the array
 
I set a (var selVal) in global and would like to automatically set "selectedIndex" for
the corresponding select option at form onLoad

smtng like:

<script>
function setSelEcho() {
var selVal = 'foo3';
var inputs = document.getElementsByName('objSel');
for(var i=0; i < inputs.length; i++) {      
        if(inputs.option[i].value == selVal ) {
                inputs.option[i].selectedIndex= true;
          }      
     }
}
</script>

<body onLoad="setSelEcho();">
<form>
<select id="objSel" name="objSel">
<option value="foo2">foo2</option >
<option value="foo3">foo3</option >
<option value="foo4">foo4</option >
</select>
</form>
</dody>
0
 
LVL 12

Accepted Solution

by:
netsmithcentral earned 1500 total points
ID: 17771016
<script>
function setSelEcho() {
var selVal = 'foo3';
var inputs = document.getElementsByName('objSel');
for(var i=0; i < inputs.length; i++) {      
        if(inputs[0].option[i].value == selVal ) {
                inputs[0].option[i].selectedIndex= true;
          }      
     }
}
</script>

<body onLoad="setSelEcho();">
<form>
<select id="objSel" name="objSel">
<option value="foo2">foo2</option >
<option value="foo3">foo3</option >
<option value="foo4">foo4</option >
</select>
</form>
</dody>
0
 

Author Comment

by:JoeUS
ID: 17773949
my inputs.length still returning 1
any idea why?
0
 

Author Comment

by:JoeUS
ID: 17775891
//I did work with this one quite hard and don't want anyone to spend the time on this more than
it is absolutely necessary

Thank you netsmithcentral for the hint, it finally lead me to the solution

<html>
<script language="JavaScript" type="text/javascript">
function setSelEcho() {
var selVal = 'foo3';
var inputs = document.getElementsByName('objSel');

//Important!!! to go after the "inputs[0]" or else will not find the object and see the options
//I am not sure if it has anything to do with multiple forms on the same page
var nops = inputs[0].options.length;

for(var i=0; i < nops; i++) {
        if(inputs[0].options[i].value == selVal ) {
                inputs[0].options[i].selected=true;
          }      
     }
}
</script>

<body onLoad="setSelEcho();">
<form>
<select id="objSel" name="objSel">
<option value="foo2">foo2</option >
<option value="foo3">foo3</option >
<option value="foo4">foo4</option >
</select>
</form>


</dody>
</html>
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

649 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