var x = $(this).parent().parent().parent().parent();
x.id gives me undefined while x.attr('id') returns the correct id.
Can anyone tell me the difference between those two.
btw, is there a better way to get $(this).parent().parent().parent().parent(), I don’t want to assign ids to them since this would make it harder to work with those elements.
Thanks
x.idworks whenxis a DOM element, not whenxis a jQuery object.If
xis a jQuery object, you can either do:to get the first DOM element from the jQuery object or you can use:
to use the jQuery method for retrieving an attribute.
As for your other question, the better way to replace this:
is to put a class on the desired parent and use this:
The
.closest(selector)method will find the nearest parent that matches the selector. Using a class to solve this issue makes it easy to design since you don’t have to use unique IDs.