Forum Moderators: open

Message Too Old, No Replies

Select onchange works in Firefox, not in IE

         

cameraguy

10:16 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



This script works just fine in Firefox, but not with IE. Can you help me find out why?

Thank you!


<script language="javascript">
function switch_language()
{
if (document.form1.select.value == "English") {window.location.href = "http://www...";}
if (document.form1.select.value == "Français") {window.location.href = "http://www...";}
}
</script>

<form name="form1" method="post" action="">
<select name="select" onChange="switch_language()">
<option selected>Français</option>
<option>English</option>
</select>
</form>

ChadSEO

11:01 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Okay, we have a couple issues here. First off, you haven't specified a value for your select options, you would want to do something like this:
<form name="form1" method="post" action="">
<select name="select" onChange="switch_language()">
<option value="Français" selected>Français</option>
<option value="English">English</option>
</select>
</form>

Secondly, to get the value of the selected item, it will be something like this:

function switch_language()
{
if (document.form1.select.options[document.form1.select.selectedIndex].text == "English") {location.href = "http://www...";}
if (document.form1.select.options[document.form1.select.selectedIndex].text == "Français") {location.href = "http://www...";}
}

Note in that example, I used .text instead of .value. This will work with what you have now, if you don't want to use the value="".

Rambo Tribble

1:42 am on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, with IE 4 and later, as well as Netscape 6 and later, the select's value should be the same as the value obtained by searching out selectedIndex. IE requires that the value be stated with a name/value attribute pair, while Netscape will accept the text entry within the option tag in lieu of a stated value.

The use of the text property has its merits, however.

Rambo Tribble

2:18 am on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might find this a simplified approach:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function switch_language(lang_val){
if(lang_val == "en"){
window.location.href="http. . .";
return;
}
if(lang_val == "fr"){
window.locaton.href="http. . .";
}
return;
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<div>
<select name="select1" onchange="switch_language(this.value)">
<option selected value="fr">Français</option>
<option value="en">English</option>
</select>
</div>
</form>
</body>
</html>

cameraguy

8:09 am on Jun 8, 2005 (gmt 0)

10+ Year Member



Thank you ChadSEO and Rambo Tribble

Rambo Tribble: I would have preferred your solution for its simplicity, but does'nt work. While it detects the change of selection (I checked with an alert), it does not jump to the required URL.

ChadSEO: I ended up using your solution, which obviously points out to a few mistakes I made.

Thank you both again!

Rambo Tribble

1:24 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason for the failure was a typo on window.location in the line for fr.

Rambo Tribble

2:16 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



However, you should be aware that onchange doesn't register if the selection doesn't change. Simply re-selecting the current selection will not fire onchange.

cameraguy

2:32 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



Thank you Rambo_Tribble.

I oversaw that type and now your solution works.

You comment regarding the onchange status sounds obvious, but needs to be remembered. In fact, I had to properly place 'option selected' on the different pages, otherwise it would not work.