javascript - Class not working jQuery -


so more 1 dynamicly generated elements same class name trying check input in jquery. instead of letting me click on both, letting me click first element generated.

ex: click on item_1 , returns item_1 id, when click on item_2 doesn't return anyting.

html

    <div id="item_1" class="resp"></div>     <div id="item_2" class="resp"></div> 

js - jquery

    $(".resp").on("click",() =>{       var id = $(".resp").attr("id");       console.log('attempting toggle' + id);     }); 

firstly, have use normal function instead of arrow function (to avoid missing context). secondly - use this keyword refer clicked element.

$(".resp").on("click", function() {    var id = this.id;    console.log('attempting toggle' + id);  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="item_1" class="resp">a</div>  <div id="item_2" class="resp">b</div>


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -