Monthly Archives: May 2010

How-To: Find tag name in jQuery

jQuery’s .attr() function makes it simple to find every attribute of a tag. ┬áBut what about the tag name?

Turns out, it’s just as easy:

this.tagName

update: This is a JavaScript function associated with a click event. You cannot access the tagName from a selector.

update 2: you can get the tag name in a loop like this:

this.nodeName

update 3: Here’s a way to check if the element matches a type. This doesn’t actually return the element type, it is just a way to check if it matches a tag name. This method returns true or false.

$('.selector').is('p')

This will also work inside a click listener method:

$(event.target).is('input')