code for select box is not working in mozilla firefox | Familiar Sight | | Join Date: Nov 2007
Posts: 240
| |
hi
I am passing the value of select to a function which is called on click event of button. here is the code -
<select name="ctName" style="width:300px;" id="clientname" >
-
<option selected="selected" value="">----Select Client Name----</option>
-
<option value="mukesh">mukesh</option>
-
<option value="kumar">kumar</option>
-
<option value="mishra">mishra</option>
-
-
<input type="button" name="Submit2" value="GO" id="bgo" onclick="advanceResults(ctName.options[ctName.selectedIndex].value" />
-
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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | 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
| | | re: code for select box is not working in mozilla firefox Quote:
Originally Posted by Dormilich 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 -
<head>
-
<script src="comsel.js" type="text/javascript"></script>
-
<meta name="Content-Script-Type" content="text/javascript" />
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>demo</title>
-
</head>
-
<select name="sel" >
-
<option selected="selected">select</option>
-
<option value="mukesh">mukesh</option>
-
<option value="kumar">kumar</option>
-
<option value="mishra">mishra</option>
-
</select>
-
<input type="button" onclick="change(sel.options[sel.selectedIndex].value)" value="submit" />
-
<span id="searchResult"></span>
-
-
var xmlHttp
-
function change(str)
-
{
-
xmlHttp=GetXmlHttpObject()
-
if (xmlHttp==null)
-
{
-
alert ("Browser does not support HTTP Request");
-
return;
-
}
-
var url="comsell.php";
-
url=url+"?q="+str;
-
url=url+"&sid="+Math.random();
-
xmlHttp.onreadystatechange=stateChanged ;
-
xmlHttp.open("GET",url,true);
-
xmlHttp.send(null);
-
}
-
function stateChanged()
-
{
-
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
-
{
-
document.getElementById("searchResult").innerHTML=xmlHttp.responseText;
-
}
-
}
-
function GetXmlHttpObject()
-
{
-
var xmlHttp=null;
-
try
-
{
-
xmlHttp=new XMLHttpRequest();
-
}
-
catch (e)
-
{
-
try
-
{
-
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
-
}
-
catch (e)
-
{
-
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
}
-
return xmlHttp;
-
}
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: code for select box is not working in mozilla firefox - 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
| | | re: code for select box is not working in mozilla firefox Quote:
Originally Posted by Dormilich - 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
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: code for select box is not working in mozilla firefox Quote:
Originally Posted by mukeshrasm 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: - var sel = document.getElementsByName("sel")[0];
-
sel.options[sel.selectedIndex].value
| | Familiar Sight | | Join Date: Nov 2007
Posts: 240
| | | re: code for select box is not working in mozilla firefox Quote:
Originally Posted by Dormilich kind of expected that. if you don't have a form element try: - var sel = document.getElementsByName("sel")[0];
-
sel.options[sel.selectedIndex].value
I changed the code like - document.getElementsByName('sel')[0].options[sel.selectedIndex].value
-
but still it is not working since this is inside the function so I have to change the double to single quote
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | re: code for select box is not working in mozilla firefox Quote:
Originally Posted by mukeshrasm I changed the code like - 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.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,642
| | | 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) -
function change()
-
{
-
var sel = document.getElementsByName("sel")[0];
-
var str = sel.options[sel.selectedIndex].value;
-
// …
-
}
-
-
// replace onclick="…" by id="submit" in the submit button
-
document.getElementById("submit").addEventListener("click", change, false);
-
// 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
| | | re: code for select box is not working in mozilla firefox Quote:
Originally Posted by Dormilich you could avoid this problem by using an external script file (though that requires the use of event listeners) -
function change()
-
{
-
var sel = document.getElementsByName("sel")[0];
-
var str = sel.options[sel.selectedIndex].value;
-
// …
-
}
-
-
// replace onclick="…" by id="submit" in the submit button
-
document.getElementById("submit").addEventListener("click", change, false);
-
// 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.
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,382 network members.
|