Yugaprashanthan
Yugaprashanthan

Reputation: 39

How to access the clicked element of class

I have buttons inside for each loop which will generate automatically when the product is added so i cannot use id to access So using the class name in java script to access fire the click event but when i click on the button only first one gets fired

**HTML CODE**

<button type="button" id="remove_item"><i class="fa fa-times remove">
</i>Remove</button>

**JS CODE**

<script>
$('.remove').click(function(e){

e.preventDefault();

     $('#decideModal').modal('show');

});
</script>

Upvotes: 0

Views: 64

Answers (1)

Bfcm
Bfcm

Reputation: 2746

You have to replace .click() with .delegate() function:

$(document).delegate('.remove', 'click', function(){
    e.preventDefault();
    $('#decideModal').modal('show');
});

And also in HTML, id refers to a unique identifier. In other words, it is against standards to have 2 elements with the same id. For example you can add counter to your element id in foreach() loop.

Upvotes: 1

Related Questions