Solved

change the selected item in a dropdownlist in Mozilla

Posted on 2007-04-10
3
317 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

758 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