Patrick Jeon
Patrick Jeon

Reputation: 1814

jquery to select parents node

To find form element from the element which triggered event. I use following code. but it seems crazy to me.

Is there a better way doing this?

$("#xx").click(function(event) {
    var testValue = $(this).parent().parent().parent().parent().parent().parent().parent().data("test");
});

thank you in advance.

Upvotes: 1

Views: 93

Answers (3)

Thulasiram
Thulasiram

Reputation: 8552

$("#xx").click(function(event) {
    $(this).parents('#YourId').attr("firstForm");
});

for reference see this link : http://api.jquery.com/parents/

Upvotes: 0

Rune FS
Rune FS

Reputation: 21742

If your HTML is valid then simply do

$("#firstForm") 

ids are unique (assuming valid HTML)

Upvotes: 1

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14747

Crazy is right. :D

$(this).closest('#firstForm') should do this for you.

Upvotes: 4

Related Questions