Reputation: 3998
I have a requirement where I print all the email addresses in <input type="text" value="emailaddress">
on the jsp page. I should be able to edit the field, so I have a button at the end of it. But the email addresses can be of any number. How to select the button ending with any number
$('#button_1').on("click", function() {
//enter code here
});
$('#button_2').on("click", function() {
//enter code here
});
I am trying to do something like
$('#button_'[]).on("click", function() {
});
Can someone help me with this solution?
Upvotes: 1
Views: 256
Reputation: 7597
CSS wildcard:
$("button[id^='button_']").click(function() {
// button elements with id starting with "button_"
// return($(this).attr('id'));
});
$("button[id*='button_']").click(function() {
// button elements with id containing "button_"
// return($(this).attr('id'));
});
Upvotes: 3
Reputation: 276125
Also instead of giving an Id you can put a class on these buttons since class can be duplicated:
<button class='email' id="#button_1"/>
<button class='email' id="#button_2"/>
Then its easier to select only these:
$('.email').on("click", function() {
// get access to the current button via this:
$(this).attr("value","delete");
});
Upvotes: 1
Reputation: 2759
There is a starts-with attribute selector.
You can do the following:
$('[id^="button_"]').on('click', function () {
// your code here
});
The $('[id^="button_"]')
selects all the elements that have an id beginning with button_
. Then you are able to bind a single handler to cater for all button events.
Upvotes: 1