• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Identifying current active element

How can a javascript function identify the current active element in a document?

for instance in the following code what should I put inplace of activeOBJ?

<HTML>
<BODY>
<FORM id="frm" action="something">
  <INPUT id="input1">
  <INPUT id="input2">
</FORM>
<SCRIPT type="text/javascript">
function handler(e);
{
  if (activeOBJ == "input1") {
    dosomething();
  }
}
document.onkeypress = handler;
</SCRIPT>
</BODY>
</HTML>
0
mehran
Asked:
mehran
  • 3
  • 2
  • 2
1 Solution
 
newjackCommented:

I would do it like this :

Put the handler function in an event of the objects, e.g. the onClick, onChange event.
By doing that you can use the 'self' object in your handler function to reference the current object.

so to check the value you could use

function handler () {
  if (self.value == "x") {
    dosomething ();
  }
}
0
 
Michel PlungjanIT ExpertCommented:
Methinks you mean this and not self

Michel
0
 
mehranAuthor Commented:
event.srcElement.name was what I really look for.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Michel PlungjanIT ExpertCommented:
Which is e.target.name in Netscape
0
 
mehranAuthor Commented:
thanks mplungjan, but I asked this question twice, once in html folder and other one in javascript folder.

and I got my answer in the other side(folder).
0
 
Michel PlungjanIT ExpertCommented:
No problem
0
 
newjackCommented:

it is indeed this and not self, I happened to come across it myself today :)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now