Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript alert box with confirm on button press

I have this link:

<p id="accept-favor"><a title="Accept this Favor" href="?wp_accept_favor=<?php comment_ID(); ?>">Accept this Favor</a></p>

I want to show a JavaScript alert box when a user clicks it saying: "Are you sure you would like to accept this reply as your favor?" with two buttons one saying "Yes" which will allow the function to run and the other saying "No" which will just cancel the postback and keep the user on the page.

How would I do this? Thanks :)

like image 983
Cameron Avatar asked Feb 10 '11 01:02

Cameron


People also ask

How can write confirm box in JavaScript?

Syntax. window. confirm("sometext"); The window.

What is the difference between an alert box and a confirmation box?

Alert box is used if we want the information comes through to the user. Confirm box is used if we want the user to verify or accept something. 2. You need to click “OK” to proceed when an alert box pops up.

How do you ask the user to accept something by clicking on a OK button or not accept by clicking on a cancel button?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

Which JavaScript dialog box will be used to get confirmation from the user?

A confirmation dialog box is mostly used to take user's consent on any option. It displays a dialog box with two buttons: OK and Cancel. If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false.


2 Answers

You can easily do it with a confirm onclick:

<p id="accept-favor"><a title="Accept this Favor"    href="?wp_accept_favor=<?php comment_ID(); ?>"    onclick="return confirm('Are you sure you would like to accept this reply as your favor?');"   >Accept this Favor</a></p> 

Though this will say OK/Cancel instead of Yes/No. If you really want Yes/No, you'll have to use a custom dialog.

like image 67
Kaleb Brasee Avatar answered Oct 06 '22 14:10

Kaleb Brasee


You can write onclick="return confirm('Are you sure?');".

The confirm function shows an OK / Cancel dialog and returns true if the user clicked OK.
returning false from an onclick handler will cancel the default action of the click.

like image 39
SLaks Avatar answered Oct 06 '22 15:10

SLaks