I try to join few function with similar functionality into one. I would like to separate this function depending which button is clicked on.
$(document).on('click', '[id^=pack-payreq-toggle-], [id^=test-payreq-toggle-]', function(e) {
e.preventDefault();
if($("[id^='pack-payreq-toggle-]").length > 0) { console.log('Pack Pay Req'); }
if($("[id^='test-payreq-toggle-]").length > 0) { console.log('Test Pay Req'); }
});
I can manage my result with "has class"
, but I would like to learn how to recognize which element is pressed based on id name. Thank you for your advice in advance.
You can use $(this).attr('id')
inside the click function so that you can check the part of that value to contain pack
or test
string in the id
using something like:
var idVal = $(this).attr('id');
if(idVal.indexOf('pack') !== -1){
console.log('Pack Pay Req');
}
if(idVal.indexOf('test') !== -1){
console.log('Test Pay Req');
}
This is because if the value test
or pack
exist in the string then it will have index from 0
to idVal.length-1
. So, we can know that value exist in the id
by checking the index not equal (or greater than) -1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.