Search code examples
jquerytagname

accessing TagName of jquery object


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

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

This alert shows me undefined. Whats wrong i am doing?


Solution

  • tagName is a property of the underlying DOM element, not an attribute, so you can use prop, which is the jQuery method for accessing/modifying properties:

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

    Better, however, is to directly access the DOM property:

    alert(obj[0].tagName);