Connecting Tech Pros Worldwide Help | Site Map

code for select box is not working in mozilla firefox

Familiar Sight
 
Join Date: Nov 2007
Posts: 240
#1: Jun 30 '09
hi

I am passing the value of select to a function which is called on click event of button. here is the code

Expand|Select|Wrap|Line Numbers
  1. <select name="ctName" style="width:300px;" id="clientname" >
  2.                   <option selected="selected" value="">----Select Client Name----</option>
  3. <option  value="mukesh">mukesh</option>
  4. <option  value="kumar">kumar</option>
  5. <option  value="mishra">mishra</option>
  6.  
  7. <input type="button" name="Submit2" value="GO" id="bgo" onclick="advanceResults(ctName.options[ctName.selectedIndex].value" />
  8.  
Here I am using ajax and function advanceResults() is called in .js page. though this code works fine in IE but it is not working in Mozilla Firefox.

and same is happening when I am passing value of Text box to other button
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#2: Jun 30 '09

re: code for select box is not working in mozilla firefox


out of the blue I'd say your JS function is too IE specific, but to be sure I need to see the source code.
Familiar Sight
 
Join Date: Nov 2007
Posts: 240
#3: Jun 30 '09

re: code for select box is not working in mozilla firefox


Quote:

Originally Posted by Dormilich View Post

out of the blue I'd say your JS function is too IE specific, but to be sure I need to see the source code.

here is the source code

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script src="comsel.js" type="text/javascript"></script>
  3. <meta name="Content-Script-Type" content="text/javascript" />
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>demo</title>
  6. </head>
  7. <select name="sel" >
  8.   <option selected="selected">select</option>
  9.   <option value="mukesh">mukesh</option>
  10.   <option value="kumar">kumar</option>
  11.   <option value="mishra">mishra</option>
  12. </select>
  13. <input type="button" onclick="change(sel.options[sel.selectedIndex].value)" value="submit" />
  14. <span id="searchResult"></span>
  15.  
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp
  2. function change(str)
  3. {
  4.     xmlHttp=GetXmlHttpObject()
  5.     if (xmlHttp==null)
  6.      {
  7.          alert ("Browser does not support HTTP Request");
  8.          return;
  9.      }
  10.     var url="comsell.php";
  11.     url=url+"?q="+str;
  12.     url=url+"&sid="+Math.random();
  13.     xmlHttp.onreadystatechange=stateChanged ;
  14.     xmlHttp.open("GET",url,true);
  15.     xmlHttp.send(null);
  16. }
  17. function stateChanged() 
  18.     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  19.      { 
  20.          document.getElementById("searchResult").innerHTML=xmlHttp.responseText;
  21.      } 
  22. }
  23. function GetXmlHttpObject()
  24. {
  25.     var xmlHttp=null;
  26.     try
  27.      {
  28.          xmlHttp=new XMLHttpRequest();
  29.      }
  30.     catch (e)
  31.      {
  32.          try
  33.           {
  34.               xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  35.           }
  36.          catch (e)
  37.           {
  38.               xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  39.           }
  40.       }
  41.     return xmlHttp;
  42. }
  43.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#4: Jun 30 '09

re: code for select box is not working in mozilla firefox


Expand|Select|Wrap|Line Numbers
  1. sel.options[sel.selectedIndex].value
what does that return in FF?

PS: the correct use of code tags is decribed here
Familiar Sight
 
Join Date: Nov 2007
Posts: 240
#5: Jun 30 '09

re: code for select box is not working in mozilla firefox


Quote:

Originally Posted by Dormilich View Post

Expand|Select|Wrap|Line Numbers
  1. sel.options[sel.selectedIndex].value
what does that return in FF?

PS: the correct use of code tags is decribed here

it returns nothing in Firefox but gives
Error: sel is not defined
Source File: http://localhost/comsel.html
Line: 1
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#6: Jun 30 '09

re: code for select box is not working in mozilla firefox


Quote:

Originally Posted by mukeshrasm View Post

it returns nothing in Firefox but gives
Error: sel is not defined

kind of expected that. if you don't have a form element try:
Expand|Select|Wrap|Line Numbers
  1. var sel = document.getElementsByName("sel")[0];
  2. sel.options[sel.selectedIndex].value
Familiar Sight
 
Join Date: Nov 2007
Posts: 240
#7: Jun 30 '09

re: code for select box is not working in mozilla firefox


Quote:

Originally Posted by Dormilich View Post

kind of expected that. if you don't have a form element try:

Expand|Select|Wrap|Line Numbers
  1. var sel = document.getElementsByName("sel")[0];
  2. sel.options[sel.selectedIndex].value

I changed the code like

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('sel')[0].options[sel.selectedIndex].value
  2.  
but still it is not working since this is inside the function so I have to change the double to single quote
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#8: Jun 30 '09

re: code for select box is not working in mozilla firefox


Quote:

Originally Posted by mukeshrasm View Post

I changed the code like

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('sel')[0].options[sel.selectedIndex].value
but still it is not working

same reason as above. you have sel not defined (you use this var twice!) that's why I used to define sel beforehand.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#9: Jun 30 '09

re: code for select box is not working in mozilla firefox


you could avoid this problem by using an external script file (though that requires the use of event listeners)

Expand|Select|Wrap|Line Numbers
  1. function change()
  2. {
  3.     var sel = document.getElementsByName("sel")[0];
  4.     var str = sel.options[sel.selectedIndex].value;
  5.     // …
  6. }
  7.  
  8. // replace onclick="…" by id="submit" in the submit button
  9. document.getElementById("submit").addEventListener("click", change, false);
  10. // disclaimer: this won't work in IE, because they don't conform to the standards, google for the addEvent() cross-browser event listeners
Familiar Sight
 
Join Date: Nov 2007
Posts: 240
#10: Jun 30 '09

re: code for select box is not working in mozilla firefox


Quote:

Originally Posted by Dormilich View Post

you could avoid this problem by using an external script file (though that requires the use of event listeners)

Expand|Select|Wrap|Line Numbers
  1. function change()
  2. {
  3.     var sel = document.getElementsByName("sel")[0];
  4.     var str = sel.options[sel.selectedIndex].value;
  5.     // …
  6. }
  7.  
  8. // replace onclick="…" by id="submit" in the submit button
  9. document.getElementById("submit").addEventListener("click", change, false);
  10. // disclaimer: this won't work in IE, because they don't conform to the standards, google for the addEvent() cross-browser event listeners

thanks for kind effort to resolve the problem and for suggestion.
Reply