Reputation: 157
<input type = "button" onclick="test()">
Js sample code:
$('input[type=button]').on('click',(function(e){
e.preventDefault();
if(!window.confirm(message)) {
return false;
}
}));
I need to stop execution test()
for other purposes when click on button. But current situation is it also calling test()
instead of return false
. Can anyone give suggestion?
Upvotes: 0
Views: 2571
Reputation: 4819
You could remove the onclick event from the HTML markup and add it manually to your code if the user does confirm. It requires you to know which function is associated to the onclick markup attribute and may be complicated if it changes depending on the circumstances. Would this work for you?
// we remove the onclick attribute from the HTML
$('input[type=button]').removeAttribute('onclick');
$('input[type=button]').on('click',(function(e){
if(!window.confirm(message)) {
// if user does not confirm, do nothing
return false;
} else {
// if user does confirm, then proceed with the test() function
test();
}
}));
Upvotes: 1
Reputation: 1464
Try to first unbind the previously active onclick:
$('input[type=button]').unbind( 'click' );
$('input[type=button]').on('click',(function(e){
e.preventDefault();
if(!window.confirm(message)) {
return false;
}
}));
Upvotes: 0