0

when append to add elements , class in elements is not working

<script type="text/javascript">
$( document ).ready(function() {
$('#click').click(function (){
   $('.products').append('<input type="checkbox" class="location-items" name="vehicle1" value="Bike">');
});

$('.location-items').click(function (){
   if($(this).is(':checked')){ 
     alert('Not working');
   }
});
});
</script>

when I click checkbox is not alert

new user
  • 1
  • 1
  • Your issue is a common one: `$('.location-items').click` only applies to elements that exist when that code runs - as you add *new* `.location-items` via the `#click`, they're not affected by the initial `$('.location-items').click` so don't get the event. You have to use **event delegation**. Change `$('.location-items').click` to `$(document).on("click", ".location-items", function() {...` – freedomn-m Apr 22 '22 at 17:21
  • It work, Thank you so much – new user Apr 22 '22 at 17:26

0 Answers0