Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate "Ok" pressed in a confirm message?

Tags:

javascript

Is there a way to simulate the Enter key on a confirm box?

The type of box created by confirm("Press a button!");

like image 762
Baldráni Avatar asked Oct 29 '25 05:10

Baldráni


1 Answers

From JavaScript, running within the page, not really.

The standard browser confirm dialog is modal and blocking. Absolutely no JS will run in the page while it is sitting there.

The closest you could come would be to override the function entirely.

function yes() {
  document.body.appendChild(document.createTextNode("You said yes! "));
}

function sure() {
  if (confirm("Are you sure?")) {
    yes();
  }
}

document.querySelector("button").addEventListener("click", sure);

window.confirm = function myConfirm() {
  return true;
}
<button>Confirm</button>
like image 184
Quentin Avatar answered Oct 30 '25 22:10

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!