HighDots Forums  

Detecting what has focus

Javascript JavaScript language (comp.lang.javascript)


Discuss Detecting what has focus in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Fabian
 
Posts: n/a

Default Detecting what has focus - 11-05-2003 , 06:52 AM






Is there a way to detect which object currently has the focus in
javascript? "this" comes close, but isnt implemented in netscape.


--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk


Reply With Quote
  #2  
Old   
Lee
 
Posts: n/a

Default Re: Detecting what has focus - 11-05-2003 , 01:34 PM






Fabian said:
Quote:
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)">



Reply With Quote
  #3  
Old   
Fabian
 
Posts: n/a

Default Re: Detecting what has focus - 11-05-2003 , 02:18 PM



Lee hu kiteb:

Quote:
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. 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?


--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk



Reply With Quote
  #4  
Old   
Lee
 
Posts: n/a

Default Re: Detecting what has focus - 11-05-2003 , 03:12 PM



Fabian said:
Quote:
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.

Quote:
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>



Reply With Quote
  #5  
Old   
Lee
 
Posts: n/a

Default Re: Detecting what has focus (correction) - 11-05-2003 , 03:47 PM



Lee said:

Quote:
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.
That last line should begin "is an event handler."



Reply With Quote
  #6  
Old   
Fabian
 
Posts: n/a

Default Re: Detecting what has focus (correction) - 11-05-2003 , 05:25 PM



Lee hu kiteb:

Quote:
Lee said:

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.

That last line should begin "is an event handler."
This is what i have now...

function Sho(ref) {
var moo = ref.parentElement.id;
// broken under mozilla
alert( moo );

// show other
// var suFFix = (/_ok/i.test( moo.slice(-3) )) ? '_no' :'_ok';
// document.getElementById(moo.slice(0,-3)+suFFix).style.display =
'block';
// hide self
ref.parentElement.style.display='none';
return null;
}

-

<DIV ID="1_ok" CLASS="mufti">
<A HREF="javascript:Xejn()" onclick="Sho(this)">text</A></DIV>

<DIV ID="1_no">
<A HREF="javascript:Xejn()" onclick="Sho(this)">text</A></DIV>

-

I have a feeling netscape cant find the parent element id because the
actual referring element (this) has no id specifically assigned. Am I on
the right track?


--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk




Reply With Quote
  #7  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Detecting what has focus - 11-05-2003 , 05:50 PM



"Fabian" <lajzar (AT) hotmail (DOT) com> writes:

Quote:
Is there a way to detect which object currently has the focus in
javascript?
Not generally, no.

However, you can tell when an element gains the focus, because
that sets off its onfocus event handler. Not all elements can
gain focus (form controls and links are the only ones AFAIK).

Quote:
"this" comes close, but isnt implemented in netscape.
"this" is a keyword that gives you the object of which the currently
executed function is a method (or the global object if there is none).

It is completely unrelated to focus. Are you sure you really mean
focus?

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #8  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Detecting what has focus (correction) - 11-05-2003 , 09:13 PM



"Fabian" <lajzar (AT) hotmail (DOT) com> wrote

<snip>
Quote:
function Sho(ref) {
var moo = ref.parentElement.id;
// broken under mozilla
snip

Not broken under Mozilla, just not implemented. parentElement is part
of the Microsoft proprietary DOM. The nearest W3C DOM equivalent is
parentNode, which is implemented in IE browsers from IE 5.0 in addition
to Mozilla and most other modern browsers. parentElement can be used to
provide fall-back for IE 4, and there are some browsers that do not
implement either so some checking should be implemented within the code
rather that assuming that the function body will execute everywhere.

Richard.




Reply With Quote
  #9  
Old   
keyur shah
 
Posts: n/a

Default Re: Detecting what has focus - 11-06-2003 , 01:31 AM



Yes, you can print this.id... it would reference the current
element/object and help u resolve ur issue.



Keyur Shah
Verizon Communications
732-423-0745

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
  #10  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Detecting what has focus - 11-12-2003 , 08:06 AM



Fabian wrote:

Quote:
it seems to me that "this" is msie-specific,
No, it is part of the core language.

Quote:
or at best, differently implemented. If it wasnt, my existing script
would work in netscape. It looks like "this" doesnt work in netscape in
the context of a function; only in a html tag.
`this' is a reference to the current context object. Within event handlers,
that is the object that triggered the event. Within methods, that is the
object that has the method as its property. Within ordinary functions, that
is the global object -- in all HTTP-UAs I know of, the current Window object.

So it probably works in the function but does not reference what you assume
it does.


HTH

PointedEars


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.