N
N
Neocaridina2020-04-18 17:42:17
JavaScript
Neocaridina, 2020-04-18 17:42:17

How to add an event to every form on the page?

For example, to assign the same onsubmit="alert('123')" to all forms on the page at one time

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-18
@Neocaridina

for (const form of document.querySelectorAll('form')) {
    form.addEventListener('submit', (event) => {
      
       event.preventDefault()
       alert('123')

    }); 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question