[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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