PDA

View Full Version : Focus is not not working in FF



msg2ajay
07-06-2007, 10:01 AM
hi,

Below is the code which is working for IE but not working for FF...
can any bady tel me how to set focus for FF... as shown below. The below there is to focus() where 'all' is wroking for IE and FORMS[] is not working for FF..




<html>
<head>
<script language="JavaScript">

function checkme(strVal){
var id=strVal;
alert(id);
//document.all[strVal].focus();---->(IE) FOR IE it is working but not working for mozilla firefox
document.forms[strVal].focus(); //(FIREFOX)
}
</script>
</head>
<body>
<form>
NAME..<input type="text" name="empName" size="5" maxlength="4" onChange="checkme(this.value)"/><br>
ID....<input type="text" name="id" value=""/><br>
DEPT..<input type="text" name="dept" value=""/><br>
</form>
</body>
</html>


thx,
Ajay.

DimX
07-06-2007, 10:22 AM
document.getElementById(strVal).focus();

Twey
07-06-2007, 01:56 PM
Your HTML is invalid (http://validator.w3.org/). It should validate as HTML 4.01 Strict for most applications.

DimX, your code will not work with that markup. The element does not have an ID, so getElementById() should not find it (although it will in IE, another bug).

msg2ajay, the easiest way to do it is to use DimX' code, and give your element an ID. With your existing excuse for markup, you can do:
document.forms[0].elements[strVal].focus();... but since your document is so invalid I wouldn't advise it.

Yogesh_gat
02-04-2011, 07:35 AM
The control “empName” has a event “onChange="checkme(this.value)” which passes value of the object. Whereas “focus()” needs object to be focused.
So to focus any object you need to pass it’s object as “onChange="checkme(this);”

May be it will help to you.:)




<html>
<head>
<script language="JavaScript">

function checkme(strVal){
var id=strVal.id;
alert(id);
//document.all[strVal].focus();---->(IE) FOR IE it is working but not working for mozilla firefox
document.forms[strVal].focus(); //(FIREFOX)
}
</script>
</head>
<body>
<form>
NAME..<input type="text" name="empName" size="5" maxlength="4" onChange="checkme(this)"/><br>
ID....<input type="text" name="id" value=""/><br>
DEPT..<input type="text" name="dept" value=""/><br>
</form>
</body>
</html>