javascript - Find closest element by attribute -
i have vote snippet, , i'd add disabled class other button user press. example if user vote + on id 1 post - button disabled class, not id 2 ones.
<span class="pull-right"> <a href="javascript:void(0)" class="vote" data-id="1" data-type="up">+</a> <span id="votes-1">0</span> <a href="javascript:void(0)" class="vote" data-id="1" data-type="down">-</a> </span> <span class="pull-right"> <a href="javascript:void(0)" class="vote" data-id="2" data-type="up">+</a> <span id="votes-2">0</span> <a href="javascript:void(0)" class="vote" data-id="2" data-type="down">-</a> </span>
i tried several things .closest().find() couldnt make work.
- traverse parent of clicked
.vote
element. - use .not() ,
this
exclude clicked.vote
element.
$('.vote').click(function() { var parent = $(this).parent(); $(this).removeclass('disabled'); parent.find('.vote').not(this).addclass('disabled'); });
.disabled { background: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <span class="pull-right"> <a href="javascript:void(0)" class="vote" data-id="1" data-type="up">+</a> <span id="votes-1">0</span> <a href="javascript:void(0)" class="vote" data-id="1" data-type="down">-</a> </span> <span class="pull-right"> <a href="javascript:void(0)" class="vote" data-id="2" data-type="up">+</a> <span id="votes-2">0</span> <a href="javascript:void(0)" class="vote" data-id="2" data-type="down">-</a> </span>
Comments
Post a Comment