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"?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With