Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
}
like image 263
bcmcfc Avatar asked Nov 01 '10 11:11

bcmcfc


People also ask

Can you get element by type in JavaScript?

Use the querySelectorAll() method to get all elements by type, e.g. document. querySelectorAll('input[type="text"]') . The method returns a NodeList containing the elements that match the provided selector.

What is type in jQuery?

The typeof operator when applied to the jQuery object returns the string "function" . Basically that does mean that jQuery is a function. But the typing sort of stops there.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

Use something like this:

$(".selector").click(function(event){ 
    var element = event.target.nodeName } 
like image 149
ilkin Avatar answered Nov 15 '22 22:11

ilkin