Solved

change the selected item in a dropdownlist in Mozilla

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Suggested Courses

630 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