473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Detecting what has focus

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

Jul 20 '05 #1
9 35580
Lee
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)">

Jul 20 '05 #2
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. 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

Jul 20 '05 #3
Lee
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>

Jul 20 '05 #4
Lee
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."

Jul 20 '05 #5
Lee hu kiteb:
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
Jul 20 '05 #6
"Fabian" <la****@hotmail.com> writes:
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).
"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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
"Fabian" <la****@hotmail.com> wrote in message
news:bo*************@ID-174912.news.uni-berlin.de...
<snip>
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.
Jul 20 '05 #8
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!
Jul 20 '05 #9
Fabian wrote:
it seems to me that "this" is msie-specific,
No, it is part of the core language.
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
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Erik Bethke | last post by:
Hello All, I am trying to clean up some polish bugs with the Shanghai game I am working on and I am currently stuck on trying to get the right event for detecting when the user has changed the...
1
by: Erik Bethke | last post by:
Hello All, I am trying to clean up some polish bugs with the Shanghai game I am working on and I am currently stuck on trying to get the right event for detecting when the user has changed the...
2
by: Dom Nicholas | last post by:
Hi, My question is this : how do I detect from another window which didn't create a new window whether it exists ? For example, is there a window-id's container of some sort that hangs around...
1
by: Oenone | last post by:
We have a little piece of JavaScript in one of our client's application which automatically sets the input focus to the first control on the HTML form. This works very nicely, except for in one...
1
by: Dean Slindee | last post by:
In my application there is a main form with a tabcontrol. On each page in the tabcontrol is a panel upon which is painted a "sub" form. The main form acts as the "application host", while the...
3
by: Greg | last post by:
The LostFocus event of datagrids is fired when the focus is added to a cell. How do you go about detecting it when the control as a whole has lost focus to another control? Slightly confused by...
0
by: Peter TB Brett | last post by:
Hi folks, I'm currently trying to work out how to detect when a PyGTK window receives the focus from the window manager -- I assume that it must receive some kind of X event, but I can't work...
3
by: Keith Wilby | last post by:
I have a form with 4 subforms on a tab control. How can I detect which sub/tab has the focus from the main form? Many thanks. Keith.
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.