Reputation: 1908
Can I call JavaScript function with return confirm();
in HTML onclick event or do I need to do function which contains confirmation and call to another function?
<button onclick="return confirm('Are you sure?'); saveandsubmit(event);"></button>
Thanks in advance.
Upvotes: 11
Views: 33824
Reputation: 2248
Try below :
<button onclick="confirm('Are you sure ?') && saveAndSubmit(event)">Button</button>
function saveAndSubmit(event){
alert('saveAndSubmit called !');
}
JSFiddle : https://jsfiddle.net/nikdtu/cyt955bp/
Upvotes: 4
Reputation: 5356
Add if condition
<button onclick="if(confirm('Are you sure?')) saveandsubmit(event);"></button>
OR
<button onclick="return confirm('Are you sure?')?saveandsubmit(event):'';"></button>
Upvotes: 30