JS/jQuery - get the type of element

If for the sake of argument I'm applying a class to a <p> and an <a> and I then want to determine whether it was an <a> or a <p> that was clicked, is there a way to do it?

Example of intended use:

$(".selector").click(function(){ element = $(this).whatElementWasClicked(); // return "a" or "p"
}

4 Answers

Use something like this:

$(".selector").click(function(event){ var element = event.target.nodeName } 
0

$(this).is("a") , for example, will also work. This is useful if you're checking against a more complicated selector, not just a particular element type.

Use nodeName or tagName.

3

use tagName for getting the tag

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like