How to determine the tagName of a Jquery object?

rascal
rascal used Ask the Experts™
on
I have a need to cycle through parent objects using JQuery until I find the 2nd occurence of a table object. I start by finding a particular <form> object and work my way up from there.

I am trying to use the 'tagName' to determine when I find a <table> but each time I try and reference the tagName from a JQuery object I get 'undefined'. I understand why now, but need to know then, how to find the tagName given that I have a jquery object.

The code snippet is the code that doesn't work:
var obj = $("form.leadform");
if (obj.length)
{
    alert("Found Form. tagName = " + obj.tagName);
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
do you mean tagName() the function?

Heres an example of how to use it
if($("#thatDiv").tagName() == "div") {
    // some of your code for this div tag
}

Open in new window

Commented:
you have to do obj[0].tagName

~Ajitha
Oh wait hold on I think I did it wrong...
var obj = $("form.leadform"); 
obj.each(function(){ 
    alert("Found Form. tagName = " + this.tagName.toLowerCase()); 
});

Open in new window

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

For jQuery you should do

obj.get(0).tagName

instead of

obj[0].tagName

Author

Commented:
Thanks SleepinDevil, but I just tried that and got the error: "obj.tagName is not a function"

Author

Commented:
Sorry, didn't see your second response. Will try the .get(0).tagName now...
Sorry rascal my first comment was completely wrong... I was in lala land as I was typing it

Author

Commented:
Still fails:
 var obj = $("form.leadform");
 if (obj.length)
 {
  alert("Found Form. tagName() = " + obj.get(0).tagName());
 }
This results in a message obj.get(0).tagname is not a function

Author

Commented:
Ok, got it now, with the updated use of .get(0) I just remove the () from the tagName() and it works :)
Yup was just about to say that :P (Once again sorry about my first comment... I was wayyyy off and confused you)

Author

Commented:
Thanks SleepinDevil, still learning the wonders of JQuery - the get(0) was the missing link in my knowledge - it returns the actual DOM object which I can then perform the tagName on.
Glad to have helped!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial