javascript - Trying to get value within tags: $(this).find(" ").text() -


i want add event every card on click increment counter 1 maximum of 3. want every card have own counter.

$(".bronze").mouseup(function(e){     if (e.which === 1) {         var counter = parseint($(this).find(".bronze_counter").text(), 10)         if (counter <= 3) {             counter += 1         }     }  }); 

this template:

<div class="container">     {% card in cards %}         {% if card.type == 'silver' or card.type == 'gold' %}         <span class="gold"><img src="{{ card.thumbnail_link }}" style="width:5%"> [</span><span class="gold_counter">0</span><span>/1] </span>         {% else %}         <span class="bronze"><img src="{{ card.thumbnail_link }}" style="width:5%"> [</span><span class="bronze_counter">0</span><span>/3] </span>         {% endif %}     {% endfor %} </div> 

any tips why doesn't work?

this might looking for.

i've added alt="bronze" example only.

since ".bronze_counter" after ".bronze" , not child of it, have use .next() *

*to prove @ html below, show it's not child.

<span class="bronze">     <img src="{{ card.thumbnail_link }}" alt="bronze" style="width:5%">     [ </span> <span class="bronze_counter">     0 </span> <span>     /3]  </span> 

the reason why value nan use .find(). .find() search inside element.

working demo

$(".bronze").click(function(e) {    if (e.which === 1) {      var counter = parseint($(this).next(".bronze_counter").text(), 10)      console.log(counter)      if (counter < 3) {        counter += 1        $(this).next(".bronze_counter").text(counter)      }    }    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="container">    <span class="gold"><img src="{{ card.thumbnail_link }}" style="width:5%"> [</span><span class="gold_counter">0</span><span>/1] </span>    <span class="bronze"><img src="{{ card.thumbnail_link }}" alt="bronze" style="width:5%"> [</span><span class="bronze_counter">0</span><span>/3] </span>  </div>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -