Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery cousin element

Tags:

jquery

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

like image 473
salmane Avatar asked Apr 20 '10 21:04

salmane


People also ask

How to get next sibling element in jQuery?

jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.

How to select next element in jQuery?

The (“element + next”) selector selects in jQuery used to select the just “next” element of the specified “element”.

What does siblings do in jQuery?

The siblings() is an inbuilt method in jQuery which is used to find all siblings elements of the selected element. The siblings are those having same parent element in DOM Tree.

Which of the following jQuery method returns all siblings of the selected element?

jQuery siblings() Method The siblings() method returns all sibling elements of the selected element.


1 Answers

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.

like image 184
DA. Avatar answered Oct 26 '22 23:10

DA.