Window.event is not working in Firefox

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&lt;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&lt;count;i++)
	{
		txtLength = nm.length;
		txtValue = listItems.item(i).text;
		if (txtValue.indexOf(nm) &gt; 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;">

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

  •   &lt;input type="text" name="txt1" onKeyPress="SearchName(this,selList,event)" value="&lt;Type To Search&gt;" onClick="javascript:this.value='';" style="width:200px;"&gt;
    

Now, it’s fine in IE and FF.

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.

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

e.preventDefault();

Thank u. I got it.