Link to home
Start Free TrialLog in
Avatar of rascal
rascalFlag for United States of America

asked on

How to determine the tagName of a Jquery object?

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

Avatar of SleepinDevil
SleepinDevil

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

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

ASKER CERTIFIED SOLUTION
Avatar of SleepinDevil
SleepinDevil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rascal

ASKER

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

ASKER

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
Avatar of rascal

ASKER

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
Avatar of rascal

ASKER

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)
Avatar of rascal

ASKER

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!