sdfgg45 sdfgg45 - 4 months ago 15
Javascript Question

How to find a spesific class,and find a spesific element and add disabled attr to it

I have two buttons, one that votes no and one that votes yes, i need to disable both buttons after user have clicked.
I also want to add the color: grey to the button clicked.

So far im able to disable the one clicked but not the other one.

My code so far are:

The html code: (also runs in a loop, so there is increment on the id)

<div class="btn-group btn-group-justified">
<div class="btn-group">
<a href="#" data-id="yes" data-problem_id="1" class="problem_vote">
<button class="btn btn-no btn-vote">YES</button>
</a>
</div>
<div class="btn-group">
<a href="#" data-id="no" data-problem_id="2" class="problem_vote">
<button class="btn btn-no btn-vote">NO</button>
</a>
</div>
</div>


I have this jQuery code so far:

$('.problem_vote').on('click touchstart', function(e){

e.preventDefault();
var link = this;

$(link).prev('.btn-group-justified').find('a').attr('disabled','disabled');
$(link).find('button').css('background-color','grey');

});

Answer

If you want to disable both at once, do this:

$('.problem_vote').on('click', function(e) {
    $(this).find("button").css('background-color', 'grey');
    $(this).closest(".btn-group-justified").find("a").attr('disabled', 'disabled');
});