When you click menu, the browser reloads the page. You don’t want the browser to reload the page. The default action of the web browser when you click menu is to reload the page.
The event.preventDefault() stops the browser from doing its default action. Now when you click on menu the page doesn’t reload.
Yes, that’s what I meant to prevent. Your menu item links presumably have an empty href attribute, which will just reload the page; you can prevent this as shown above.
Another possibility would be to give those links a href="#", which will however change the URL; or no href attribute at all, which again will compromise accessibility. How does your markup look like though?
My menu responsive. When i click im ‘button responsive’ the page open my menu. When i click in links of ‘menu responsive’ my page reload. But here https://codepen.io/Coding_Journey/pen/GXYbjw this doesn’t occurs. I want to know when i am wronging.
Your menu item links have empty href attributes, which will cause the page to reload. Call event.preventDefault() in your click handler function to prevent this.
function change (event) {
event.preventDefault()
heade.classList.toggle('toggle')
}
In that pen the href attributes are not empty but point to elements within the same page, which will not cause a reload.