pt0
pt0

Reputation: 175

Detect which element clicked in foreach, jquery

How to detect specific clement, that was clicked, when the id's form all elements are the same, in the foreach loop?

In the foreach loop, there is an input:

echo " <input class='active' name='notification_id_' id='notification_id_' value='$notification_id''>$notification_id</input> <br />";

Jquery code I use detects only first value:

var id = $('#notification_id_').val();

Upvotes: 0

Views: 325

Answers (3)

bipen
bipen

Reputation: 36531

First of all, you cannot have same id for multiple element. IDs should always be unique (that's why it is called ID). Change all to class and use this reference.

Try this:

$('.className').click(function(){
    alert($(this).val());
});

Upvotes: 2

Nirmal
Nirmal

Reputation: 2368

1.simple method:

$('.className').click(function(){
alert($(this).val());
 });

2.By your method:

echo " <input class='active' name='notification_id_' id='notification_id_' value='$notification_id' onclick=test(notification_id_);>$notification_id</input> <br />";

In

function test(var val)
{
  alert(val);
}

hope this will give you some solution.

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

Try with this like and use class that you can have multiple classes with same name.but id should always be unique

$('.my_btn').on('click',function(){
    var id = $(this).val(); 
    alert("Clicked on id :" + id);
});

Upvotes: 1

Related Questions