Solved

change the selected item in a dropdownlist in Mozilla

Posted on 2007-04-10
3
321 Views
Last Modified: 2008-02-01
When an image is clicked, I want the value of the dropdown list to change.
The below (selectStyle.value = ) works for IE, but not for Mozilla.

sampleImg.Attributes.Add("onClick", " selectStyle.value = " + coverTypeRPOV.RetailerProductOptionValueID.ID + ";");
0
Comment
Question by:Gezna
[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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
mistled earned 500 total points
ID: 18885165
Are you trying to change which item is selected?  Or the actual value of a specific option in the drop down?

Assuming that selectStyle is the id of the select box....

//set the value
document.getElementById('selectStyle').options[i].value == "new value";

//set the selected option
document.getElementById('selectStyle').options[i].selected = true;
OR
document.getElementById('selectStyle').selectedindex = i;  //where i is the index of the option you want to highlight

Either way, you will need to know which item in the list is the one you want to change and replace the [i] above with it (so "options[i]" becomes "options[2]" or whatever the index is.

If you don't know which item it is and are just trying to select the option that you have a known value for, try this....

var select = document.getElementById('selectStyle');
for ( var i = 0; i < select.options.length; i++) {
      if ( select.options[i].value == value) {
            select.selectedIndex = i;
      }
}
0
 

Author Comment

by:Gezna
ID: 18885525
I'm trying to change the item that is selected (visually)
My code actually works for IE, and I tried your last function (see below) and it works in IE as well.
However, in Mozilla Firefox, neither one works and the alert won't even fire.

//C#
//*********************
sampleImg.Attributes.Add("onClick", "setStyle(" + coverTypeRPOV.RetailerProductOptionValueID.ID + ");");

//javascript
//*************************
function setStyle(value){
var select = document.getElementById('selectStyle');
for ( var i = 0; i < select.options.length; i++) {
if ( select.options[i].value == value) {
select.selectedIndex = i;
}
}
alert(select.selectedIndex);
}
0
 

Author Comment

by:Gezna
ID: 18885949
I must have been doing something wrong, because after much disecting, I managed to get your code to work in Mozilla, such that it changes the dropdownlist value
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

752 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