I have some DIVs which represent buttons, all the button as the following ID structure:
Button_<id>
Example:
Button_54
Is there a way to attach a click listener to all the button and get its ID inside the listener? I came up with the following solution, but I don’t think it is the way to do so… Do you have any suggestion?
$('[id$="Button_"]').click(function() {
var id = $(this).attr('id');
var ButtonID = id.substring(id.lastIndexOf("Button_"));
//.
//. Do somthing which requires the ID
//. of this button, for instance an AJAX request
//.
});
You are using Attribute Ends With selector, which doesn’t select your elements, you should use Attribute Starts With selector instead: