Skip to content Skip to sidebar Skip to footer

Simulate "ok" Pressed In A Confirm Message?

Is there a way to simulate the Enter key on a confirm box? The type of box created by confirm('Press a button!');

Solution 1:

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.

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

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

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

window.confirm = functionmyConfirm() {
  returntrue;
}
<button>Confirm</button>

Post a Comment for "Simulate "ok" Pressed In A Confirm Message?"