Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want it to show like a sure to continue when the button is clicked

Why doesn’t this work, I want it to show me "sure to proceed?" When I click the button to which the ".collect" class is attached.

const button = document.querySelector(".collect");
button.addEventListener("click", function() {
    alert("Sure to proceed?");
});
<button type="submit" class="collect">PROCESS</button>

Executing the JavaScript after the target element solves the problem, refer to the post anchored to @David ‘s comment. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of alert, use confirm.

I created a wrapper called navigate that executes the callback, if the user hits the "OK" button.

The performNavigation event handler calls this wrapper function.

const navigate = (message, callback) => {
  if (confirm(message) && callback) {
    callback();
  }
}

const performNavigation = () =>
  navigate("Are you sure you want to proceed?", () => {
    console.log('Proceeding...');
  });

const button = document.querySelector('.collect');

button.addEventListener('click', performNavigation);
<button class="collect">Proceed</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading