Fabian said:
Lee hu kiteb:
Fabian said:
Is there a way to detect which object currently has the focus in
javascript? "this" comes close, but isnt implemented in netscape.
Sure it is:
<input id="alpha" onfocus="alert(this.id)">
it seems to me that "this" is msie-specific, or at best, differently
implemented. If it wasnt, my existing script would work in netscape.
There are many reasons why a script might work in one browser
but not another. The real reason isn't always obvious.
It looks like "this" doesnt work in netscape in the context of a function;
only in a html tag. How can I detect what object on screen has the focus
when a particular function is being called?
The "this" keyword works in the context of a function, but in
that context, it refers to the window, unless that function
is a function handler. That's true in IE and Netscape.
In this example:
function demo(ref){
document.myForm.output1.value=ref.id;
document.myForm.output2.value=this.id;
}
<form name="myForm">
<input name="output1">
<input name="output2">
<br>
<input id="alpha" onfocus="demo(this)">
</form>
the value placed in output1 will be "alpha" and in output2 will
be "undefined", in both IE and Netscape 7.
If demo() is actually the event handler, then the "this" keyword
will refer to the element that took focus:
<head>
<script type="text/javascript">
function demo(){
document.myForm.output1.value=this.id;
}
</script>
</head>
<body onload="document.myForm.alpha.onfocus=demo">
<form name="myForm">
<input name="output1">
<br>
<input id="alpha">
</form>
</body>