Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector for child of sibling?

I'm trying to get a jQuery selector for the child of a sibling (multiple sections on the page so need to just reach the sibling's child (rather thna being able to use a general selector).

HTML:

<div class="tour-section">                 <div class="screenshots left">                     <div class="tab tab1"></div>                     <div class="tab tab2"></div>                     <div class="tab tab3"></div>                     <div class="tab tab4"></div>                 </div>                 <div class="talking-point section1">                     <h2 class="primary-color-text">Create your listing</h2>                     <p class="subheader">Create your job listing by adding a few simple details</p>                 </div> </div> 

JS:

$(".talking-point.section1").mouseenter(function() {             $(this).siblings(".screenshots").children("tab").hide();             $(".screenshots .tab1").show();             $(this).siblings(".screenshots").css("background-position","0 0");         });  
like image 738
Walker Avatar asked Aug 25 '10 07:08

Walker


People also ask

What is difference between children () and siblings ()?

Just like in real life with real families, siblings are elements with the same parent element. The elements with IDs 2, 3, and 4 are all siblings of each other, and are children of element with ID 1.

What is the use of parent () and child () method in jQuery?

The ("parent > child") selector selects all elements that are a direct child of the specified element.

How can I find siblings in jQuery?

jQuery siblings() Method Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward and backwards along siblings of DOM elements. Tip: Use the prev() or next() method to narrow down the search for only previous or next sibling elements.

How do I choose a sibling element?

Adjacent Sibling Selector (+) The adjacent sibling selector is used to select an element that is directly after another specific element. Sibling elements must have the same parent element, and "adjacent" means "immediately following".


2 Answers

Logically it should work. You're missing a dot . for the .children() Jquery selector.

.children(".tab") 
like image 94
jAndy Avatar answered Sep 21 '22 16:09

jAndy


Using find instead of children worked for me:

$(this).siblings(".screenshots").find("tab") 
like image 36
warvariuc Avatar answered Sep 19 '22 16:09

warvariuc