Search code examples
javascripthtmlsubmitaddeventlistenerpreventdefault

addEventListener with 'submit' is not working, actually keep refreshing


im trying to do submit with enter in the form tag, but its not working
here is my code

document.querySelector('#button').addEventListener('submit',(e)=>{
    e.preventDefault()
    console.log('it works')
})
<ul>
    <li>
        <div>
            <form>
                <input></input>
                <button type="submit" id='button'></button>
            </form>
        </div>
    </li>
</ul>


Solution

  • Submit events fire on form elements, not button elements.

    You're listening for the event in the wrong place.