Mr_Ram
December 30, 2005, 3:19am
1
The following code is working in IE, but not in firefox :
<script>
var prevIndex = -1;
function SearchName(txtObj,selObj)
{
var keyPressed = window.event.keyCode;
var pressedKey = String.fromCharCode(keyPressed);
var nm = txtObj.value;
var count = selObj.length;
var i;
var txtLength;
var txtValue;
if(keyPressed == 13)
{
prevIndex = -1;
return false;
}
nm += pressedKey;
var listItems = selObj;
if (prevIndex != -1)
{
listItems.item(prevIndex).selected=false;
}
if (isNaN(nm))
{
for (i=0;i<count;i++)
{
txtLength = nm.length;
txtValue = listItems.item(i).text;
if (txtValue.substr(0,txtLength).toUpperCase() == nm.toUpperCase() )
{
listItems.item(i).selected=true;
prevIndex = i;
break;
}
}
}
else
{
for (i=0;i<count;i++)
{
txtLength = nm.length;
txtValue = listItems.item(i).text;
if (txtValue.indexOf(nm) > 0)
{
listItems.item(i).selected=true;
prevIndex = i;
break;
}
}
}
}
</script>
<input type=“text” name=“txt1” onKeyPress=“SearchName(this,selList)” value=“<Type To Search>” onClick=“javascript:this.value=‘’;” style=“width:200px;”>
That’s because Firefox handles events slightly different. Instead of using window.event, you should add a parameter to the function signature, like so:
function SearchName(e, txtObj, selObj) {
evt = e || window.event;
var keyPressed = evt.which || evt.keyCode;
}
and then make your function call like so:
<input type="text" name="txt1" onKeyPress="SearchName(event,this,selList)" value="<Type To Search>" onClick="javascript:this.value='';" style="width:200px;">
Mr_Ram
December 30, 2005, 9:25am
3
Thanks
I changed the code as follows :
function SearchName(txtObj,selObj,e)
var keyPressed;
if(window.event)
keyPressed = window.event.keyCode; // IE
else
keyPressed = e.which; // Firefox
<input type="text" name="txt1" onKeyPress="SearchName(this,selList,event)" value="<Type To Search>" onClick="javascript:this.value='';" style="width:200px;">
Now, it’s fine in IE and FF.
felgall
December 30, 2005, 10:11pm
4
if(window.event)
keyPressed = window.event.keyCode; // IE hack
else
keyPressed = e.which; // standard method
The else code isn’t Firefox specific, it is the STANDARD.
Mr_Ram
August 9, 2006, 4:39am
5
It works in IE but doesn’t work in Firefox.
I have found that FF (and NN) doesn’t understud:
window.event.returnValue = false;
Pls let me know the equivalent code in FF