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.
To invoke this function in the html document, we have to create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button.
Yes, you can call two JS Function on one onClick.
We can create an HTML button element and use the native onclick event. The value of this event will be a callback function activateLasers() . So when the button is clicked, activateLasers() is also called and adds an alert to our screen.
Using onclick Event: The onclick event attribute works when the user click on the button. When mouse clicked on the button then the button acts like a link and redirect page into the given location. Using button tag inside <a> tag: This method create a button inside anchor tag.
Add if condition
<button onclick="if(confirm('Are you sure?')) saveandsubmit(event);"></button>
OR
<button onclick="return confirm('Are you sure?')?saveandsubmit(event):'';"></button>
Try below :
<button onclick="confirm('Are you sure ?') && saveAndSubmit(event)">Button</button>
function saveAndSubmit(event){
alert('saveAndSubmit called !');
}
JSFiddle : https://jsfiddle.net/nikdtu/cyt955bp/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With