Connecting Tech Pros Worldwide Forums | Help | Site Map

Novice Javascript problem with IE

Newbie
 
Join Date: Aug 2007
Posts: 3
#1: Aug 1 '07
Hi,

I have a problem with a simple piece of Javascript
Markup:
Expand|Select|Wrap|Line Numbers
  1. <label for="route">How did you find our website?:</label>
  2. <select name="nameroute" id="route" onChange="show()">
  3. <option>Search engine - Google</option>
  4. <option>Search engine - Yahoo</option>
  5. <option>Search engine - MSN</option>
  6. <option class="Link">Link from another website</option>
  7. <option class="Mag">Magazine/newspaper/brochure</option>
  8. <option class="Other">Other</option>
  9. </select><br />
  10.  
JS:
Expand|Select|Wrap|Line Numbers
  1. function show(){
  2.     var RouteInput;
  3.     RouteInput = document.getElementById("route")
  4.  
  5.     if (RouteInput.value == 'Other'){
  6.           alert("Other works!")
  7.     }    else if (RouteInput.value == 'Magazine/newspaper/brochure'){
  8.            alert("Mag works")
  9.     }     else if (RouteInput.value == 'Link from another website'){
  10.            alert("Link works");
  11.     }
  12.  
  13. }
  14.  
This works as i expected in FireFox 2, Safari 3 and Opera 9 (all win), but does not work in IE.

Can anyone tell me what I am doing wrong?


Thanks in advance!

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Aug 1 '07

re: Novice Javascript problem with IE


Welcome to TSDN!

Add the value attribute to your 'option' tags.
Newbie
 
Join Date: Aug 2007
Posts: 3
#3: Aug 1 '07

re: Novice Javascript problem with IE


Hello,

I tried that:

Expand|Select|Wrap|Line Numbers
  1. <select name="nameroute" id="route" onChange="show()">
  2. <option>Search engine - Google</option>
  3. <option>Search engine - Yahoo</option>
  4. <option>Search engine - MSN</option>
  5. <option value="Link">Link from another website</option>
  6. <option value="Mag">Magazine/newspaper/brochure</option>
  7. <option value="Other">Other</option>
  8. </select><br />
  9.  
Expand|Select|Wrap|Line Numbers
  1. function show(){
  2.     var RouteInput;
  3.     RouteInput = document.getElementById("route")
  4.  
  5.     if (RouteInput.value == 'Other'){
  6.           alert("Other works!")
  7.     }    else if (RouteInput.value == 'Mag'){
  8.            alert("Mag works")
  9.     }     else if (RouteInput.value == 'Link'){
  10.            alert("Link works");
  11.     }
  12.  
  13. }
  14.  
This still does not work in IE 6 or 7. I get an error symbol in the error console in Firefox but there is not info as to what the error is.
Newbie
 
Join Date: Aug 2007
Posts: 3
#4: Aug 1 '07

re: Novice Javascript problem with IE


No worries, twas a syntax error:

I failed to put a semi colon after this statement:
Expand|Select|Wrap|Line Numbers
  1. route = document.getElementById("route-other");

Expand|Select|Wrap|Line Numbers
  1. function show(){
  2.  
  3.     if (RouteInput.value == 'Other' || RouteInput.value == 'Mag' || RouteInput.value == 'Link') {
  4.                var route;
  5.             route = document.getElementById("route-other");
  6.             route.style.display="block";
  7.     }    else {
  8.               var route;
  9.             route = document.getElementById("route-other");
  10.             route.style.display="none";
  11.     }
  12.  
  13.  
  14. }
  15.  
Reply