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.
Loading...
Related Quiz
- The ______ statement is used to specify a block of code to be executed if the condition is false.
- When a function expression is made async, it returns a ______.
- How does JavaScript support lexical scoping?
- To remove a child element, you should use the removeChild method on the _________ element.
- You are debugging a web page and find that an element isn't being selected as expected with document.querySelector('.example'). What could be a possible reason if the class name is correct?