473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 35616
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(t his)">
</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="documen t.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.parentEleme nt.id;
// broken under mozilla
alert( moo );

// show other
// var suFFix = (/_ok/i.test( moo.slice(-3) )) ? '_no' :'_ok';
// document.getEle mentById(moo.sl ice(0,-3)+suFFix).styl e.display =
'block';
// hide self
ref.parentEleme nt.style.displa y='none';
return null;
}

-

<DIV ID="1_ok" CLASS="mufti">
<A HREF="javascrip t:Xejn()" onclick="Sho(th is)">text</A></DIV>

<DIV ID="1_no">
<A HREF="javascrip t:Xejn()" onclick="Sho(th is)">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/rasterTriangleD OM.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.parentEleme nt.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
2735
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 desktop resolution. I have tried trapping the following events: 1) SDL_ACTIVEEVENT 2) SDL_VIDEOEXPOSE
1
2862
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 desktop resolution. I have tried trapping the following events: 1) SDL_ACTIVEEVENT 2) SDL_VIDEOEXPOSE 3) SDL_VIDEORESIZE
2
4965
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 that I can interrogate ? I would like to find a better way of doing the following. I have a window (call it Win) that creates a new window (call it Win2) using window.open().
1
2826
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 circumstance. If the user is partway through entering data into the form, clicks a hyperlink to go to another page, and then clicks the Back button to return to the edit page, the focus is again reset to the first field on the page. Under this...
1
1690
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 subforms are where data values contained in underlying tables are displayed, inserted, updated or deleted. I don't make the user press an "Edit" button to enable the textboxes on the subforms. Rather, the subforms and their textboxes are always...
3
6140
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 this! Greg.
0
1808
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 out which signal it generates. Searching around the subject on the web doesn't seem to pull up anything useful. I tried the "focus" signal, but that doesn't work the way I'd like it to. For example, the following code only prints the message...
3
4961
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
4234
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 testing for support of particular eventTypes - the DOM 2 Events Interface DocumentEvent says that if the eventType is not supported, it throws a DOM exception. This makes testing rather tough - if you try something like: if (document &&...
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.