1

I have dynamically created element below. how can I attach an event on click when a button is clicked.

Ex: when button tag in ID item1 is clicked. I will get the h5 tag value. and so as item2 when button ID item2 is clicked will get the h5 tag on item2 value.

I try the ff. jquery but it return the h5 of all item1 and item2.

$('#Id').on('click','.myclass', function() {
  var text = $(this).parent().text();
  alert(text);
});


<div id="Item1" class="toolimg">
<div class="dplicon"></div>
<center>
     <h5>Item1</h5>
     <button class="btn btn-primary">Download</button>
</center>
</div>

<div id="Item2" class="toolimg">
<div class="dplicon"></div>
<center>
     <h5>Item2</h5>
     <button class="btn btn-primary">Download</button>
</center>
</div>
glen.a
  • 51
  • 3
  • $(document).on("click","#Item2",function() { alert($(this).find('h5').text()); }); – Vishnu Chauhan Aug 23 '18 at 09:41
  • 1
    Hi Vishnu - I cannot determine yet what ID to use since, it was dynamically created it could be up to item10. i want to know which button item was clicked and get the text. – glen.a Aug 24 '18 at 03:15
  • 1
    You can simply use toolimg class which is already bind with each div then you can find h5 like this $(document).on("click",".toolimg",function() { alert($(this).find('h5').text()); }); – Vishnu Chauhan Aug 24 '18 at 07:08

0 Answers0