Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select a sibling element using jQuery?

Can you help me with this jQuery selector?

$(".auctiondiv .auctiondivleftcontainer .countdown").each(function () {     var newValue = parseInt($(this).text(), 10) - 1;     $(this).text(newValue);      if (newValue == 0) {         $(this).parent().fadeOut();         chat.verify($(this).parent().parent().attr('id'));     } }); 

Basically, I want to select the element with .bidbutton class that belongs in the same parent as the .countdown in the each loop:

<div class="auctiondivleftcontainer">     <p class="countdown">0</p>     <button class="btn primary bidbutton">Lance</button>                             </div>   

And then apply this to that button:

$(button here).addClass("disabled"); $(button here).attr("disabled", ""); 
like image 477
Only Bolivian Here Avatar asked Sep 18 '11 17:09

Only Bolivian Here


People also ask

What is sibling in jQuery?

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

How can I select an element by name with jQuery?

Answer: Use the Attribute Selector You can use the CSS attribute selectors to select an HTML element by name using jQuery. The attribute selectors provide a very flexible and powerful mechanism for selecting elements.

Which function in jQuery is used to grab a previous sibling element?

jQuery prev() Method The prev() method returns the previous sibling element of the selected element. Sibling elements are elements that share the same parent.


1 Answers

Use jQuery .siblings() to select the matching sibling.

$(this).siblings('.bidbutton'); 
like image 51
Madara's Ghost Avatar answered Sep 28 '22 18:09

Madara's Ghost