jquery - onclick function is not repeating inside javascript for loop -


this question has answer here:

i want repeat jquery function dynamically n-numner of times. alert working number of count function not created many times. code below

        var count = 5;         var = 1;         (var = 1; <= count; i++)         {             var min = ".min" + i;             alert(min); // testing purpose, working             document.writeln(min); // testing purpose, working             alert(i); // testing purpose, working              // following function not repeated,              $(document).ready(function () {                 $("min" + i).click(function () {                     alert(i);                 });             });         } 

my body section code follows

 <div class="min1"> test - 1 </div>  <div class="min2"> test - 2 </div>  <div class="min3"> test - 3 </div>  <div class="min4"> test - 4 </div> 

this may simple need someone's guide done. please me make work. concept going implement on large basis in project. in advance.

although problem in code wrong use of closure in loop.

the solution can different, can use common click handler elements use data-* attribute store dynamic value needed in click handler.

$(document).ready(function() {    $(".min").click(function() {      alert($(this).data('id'));    });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <div class="min1 min" data-id="1">test - 1</div>  <div class="min2 min" data-id="2">test - 2</div>  <div class="min3 min" data-id="3">test - 3</div>  <div class="min4 min" data-id="4">test - 4</div>


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -