Only Bolivian Here
Only Bolivian Here

Reputation: 36773

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", "");

Upvotes: 101

Views: 185446

Answers (10)

Barath Kumar
Barath Kumar

Reputation: 439

$("selector").nextAll(); 
$("selector").prev(); 

you can also find an element using Jquery selector

$("h2").siblings('table').find('tr'); 

Upvotes: 4

Tarik A.
Tarik A.

Reputation: 3

If I understood that correctly you're already in a loop (each) so you would always want to select that one sibling button inside each loop runthrough? Since siblings() returns an array, this would be the way to go:

$(this).siblings('.bidbutton')[0]

You can apply both things you wanted in a single line doing this:

$(this).siblings('.bidbutton')[0].addClass("disabled").attr("disabled", "");

Upvotes: 0

Sarasjd
Sarasjd

Reputation: 197

you can use 
$(this).siblings(".bidbutton").addClass("disabled");
$(this).siblings(".bidbutton").attr("disabled","");

Upvotes: 4

Sourav Basak
Sourav Basak

Reputation: 486

$("h2").siblings().css({"color": "blue"});

Upvotes: 6

Shobi
Shobi

Reputation: 11501

also if you need to select a sibling with a name rather than the class, you could use the following

var $sibling = $(this).siblings('input[name=bidbutton]');

Upvotes: 0

Peter Meadley
Peter Meadley

Reputation: 509

If you want to select a specific sibling:

var $sibling = $(this).siblings('.bidbutton')[index];

where 'index' is the index of the specific sibling within the parent container.

Upvotes: 1

ipr101
ipr101

Reputation: 24236

Try -

   $(this).siblings(".bidbutton").addClass("disabled").attr("disabled", "");

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 175088

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

$(this).siblings('.bidbutton');

Upvotes: 178

JohnD
JohnD

Reputation: 4002

$(this).siblings(".bidbutton")

Upvotes: 9

SeanCannon
SeanCannon

Reputation: 78006

Since $(this) refers to .countdown you can use $(this).next() or $(this).next('button') more specifically.

Upvotes: 0

Related Questions