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
Post a Comment