Как в JS сделать сабмит по форме?
Сейчас у меня есть эвент клика по кнопке, а как правильно оформить этот фрагмент кода, чтобы эвент происходил через сабмит по форме?
const inputPlace = document.querySelector('.popup__input_type_place');
const inputLink = document.querySelector('.popup__input_type_link');
createCardButton.addEventListener('click', function (e) {
e.preventDefault();
const nameAndLink = {
name: inputPlace.value,
link: inputLink.value
};
cardContent(nameAndLink);
closeAllPopUps(popupAdd);
inputPlace.value = inputPlace.textContent;
inputLink.value = inputLink.textContent;
});
.popup__container{
max-width: 430px;
width: 100%;
box-sizing: border-box;
background-color: white;
color: black;
display: flex;
flex-direction: column;
position: relative;
box-shadow: 0px 0px 25px rgba(0, 0, 0, 0.15);
border-radius: 10px;
padding-bottom: 40px;
}
@media screen and (max-width: 470px){
.popup__container{
max-width: 270px;
}
}
@media screen and (max-width: 350px){
.popup__container{
max-width: 250px;
}
}
<div class="popup popup_function_add">
<form class="popup__form">
<div class ="popup__container">
<button type="button" class="popup__close popup_close_add"></button>
<h2 class="popup__edit">Новое место</h2>
<div class="popup__inputs">
<input type="text" name="place" required class="popup__input popup__input_type_place" placeholder="Место">
<input type="text" name="link" required class="popup__input popup__input_type_link" placeholder="Ссылка"></div>
<button type="submit" class="popup__save popup__save_create_card">Создать</button>
</div>
</form>
Ответы (1 шт):
Автор решения: Даниил Чижевский
→ Ссылка
Есть два варианта:
- Добавить кнопку, сделать её невидимой и вместо отправки выполнить метод
button.click(). - Выполнить метод
form.submit().