You’re developing a web application and need to add a feature where a modal appears when a button is clicked, but users complain that the page scrolls up every time they click the button. What JavaScript method could you use to prevent this default behavior?

  • event.stopPropagation()
  • event.preventDefault()
  • event.stopImmediatePropagation()
  • event.cancelBubble()
To prevent the default behavior of a button click, you can use the event.preventDefault() method. This method stops the browser from executing the default action associated with the event, such as submitting a form or navigating to a new page, in this case, preventing the page from scrolling up. event.stopPropagation() is used to stop the event from bubbling up the DOM tree but won't prevent the default behavior.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *