tags:

views:

70

answers:

1

I am trying to locate the closest element that contains a class....this is in an effort to find the "cousin" of the current element that i have.. the following did not work :

$('myelement').closest ('*:has(.class1)').find('class_cousin')

I am using the * in closest since i am not sure what type element is the one i am looking for nor do I know if it has any classes or ID ( i am trying to keep it general for a plugin)

any idea how I could do it? thank you

+1  A: 

I think you're close. Something like this should work:

$('myelement').closest(':has(.yourClass)').find('.yourClass')

I think the key is that you're looking for the closest parent that has the particular class you are looking for, then want to find that particular object.

Granted, the problem with the above is that it's not necessarily finding ONE instance of .yourClass. You could have a parent object with multiple cousin objects. I'm also not sure how expensive of a query the above is. There might be performance issues.

DA
worked like a charm.this was for a form validation plugin ( i realize there r tons of them out there but I wanted to go through the exercise and maybe make something more flexible)...I guess what i am trying to say is that performance doesnt matter much here :)I appreciate the help
salmane