rascal
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:
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);
}
you have to do obj[0].tagName
~Ajitha
~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());
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks SleepinDevil, but I just tried that and got the error: "obj.tagName is not a function"
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
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
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
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)
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!
Heres an example of how to use it
Open in new window