lingo
lingo

Reputation: 1908

How to confirm and call function with onclick

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

Answers (2)

Nikhil Maheshwari
Nikhil Maheshwari

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

Man Programmer
Man Programmer

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

Related Questions