Message from JavaScript discussions

July 2019

— JavaScript event handlers


const button = document.querySelector('...');
button.addEventListener('click', function (e) {
alert('Default behavior prevented!');

— (replace '...' with a selector to your buttons)

— Thank you😊

— It worked

— You can remove the alert or replace it with other stuff to do when button is clicked

— e.preventDefault() is the thing that stops the reload

— But there is another problem

— I used this in the button in form

— The page reload stopped

— But when I again click that button it not works!

— What do you mean by "works"