How to confirm and call a function using onclick

Is it possible to call a JavaScript function using return confirm();in the onclick HTML event, or do I need to execute a function that contains a confirmation and a call to another function?

<button onclick="return confirm('Are you sure?'); saveandsubmit(event);"></button>

Thanks in advance.

+4
source share
2 answers

Add if condition

<button onclick="if(confirm('Are you sure?')) saveandsubmit(event);"></button>

OR

<button onclick="return confirm('Are you sure?')?saveandsubmit(event):'';"></button>
+12
source

Try the following:

<button onclick="confirm('Are you sure ?') && saveAndSubmit(event)">Button</button>


function saveAndSubmit(event){
    alert('saveAndSubmit called !');
}

JSFiddle: https://jsfiddle.net/nikdtu/cyt955bp/

+1
source

Source: https://habr.com/ru/post/1605913/


All Articles