The problem is, when I click on a submit button the page is reloading, preventing the code to work, despite the fact that I have "event.preventDefault();".
HTML:
<form class="links-add-container hidden">
<input
type="text"
class="links-name-input links-input"
spellcheck="false"
placeholder="Название ссылки"
/>
<input
type="text"
class="links-link-input links-input"
spellcheck="false"
placeholder="Ссылка"
/>
<button class="links-add-button button submit-button" type="submit">
<i class="fas fa-plus-square"></i>
</button>
</form>
JS:
const addLinkButton = document.querySelector(".links-add-button");
addLinkButton.addEventListener("click", addLink);
function addLink(event) {
event.preventDefault();
//the actual code. It doesn't work because of the page reload.
}
I've tryed to change type of the button on the "button", but it leads to the other problem. The page doesn't reload but the code doesn't work. When i click on the button nothing happens, even error in the console doesn't appear.
I should add that this is my old code and when I worked with it half a year ago there was no such problem. Most likely I'm missing something obvious, but I don't understand what. Please help.
submitevent (I think)clickas preventing the button from submitting the form it's attached to. I can't find that in the byzantine HTML standard, but I suspect it's there.event.preventDefault()in a click handler really prevent the form submission. That was not the case for three years ago. Make sure the submit event is actually attached to the form, not to a submitter element.