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.

  1. traverse parent of clicked .vote element.
  2. 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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -