Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selectors (finding tag)

Tags:

jquery

I'm sure this is simple, but I can't seem to figure it out. I need to be able to pass a function an element id, and know what element tag it is.

For example:

<a id="first"></a>
<input id="last" />

If I know the id is "first", how can I get that the tag is "a"?

like image 712
Matthew Avatar asked Jan 24 '26 03:01

Matthew


1 Answers

This should do it:

var tagName = $("#first")[0].tagName;

The [0] is synonymous with get(0). You get the first element from the jQuery object and use the DOM tagName property. It's arguably more straightforward in vanilla Javascript:

var tagName = document.getElementById("first").tagName;
like image 197
cletus Avatar answered Jan 27 '26 01:01

cletus