"); alert($(obj).attr("tagName"))...">

Access TagName of jquery object

I want to know the tagName of a jquery object, I tried:

var obj = $("<div></div>"); alert($(obj).attr("tagName")); 

This warning shows me undefined . What am I not doing?

+4
source share
3 answers

tagName is a property of the underlying DOM element, not an attribute, so you can use prop , which is the jQuery property access / modification method:

 alert($(obj).prop('tagName')); 

It is better, however, to directly access the DOM property:

 alert(obj[0].tagName); 
+8
source

You need to access the base DOM node, since jQuery objects do not have the tagName property, and tagName not a property, not an attribute:

 var obj = $("<div></div>"); alert(obj[0].tagName); 

Note that I also deleted the jQuery call in the second line, since obj already a jQuery object.

+2
source

tagName is a native property of the DOM element, it is not part of jQuery itself. With that in mind, use $()[0] to get the DOM element from the jQuery selector, for example:

 var obj = $("<div></div>"); alert(obj[0].tagName); 

Script example

+1
source

Source: https://habr.com/ru/post/1414684/


All Articles