Kevin Powell - CommunityKP-C
Kevin Powell - Community4mo ago
106 replies
Ray

Preventing form submission without using the 'disabled' attribute.

I am having trouble understanding the MDN documentation of the removeEventListener function, and figured I should just ask, so here is my probably incorrect code that I can't at the moment test:

submitReady(ready){
        
        const submit = document.getElementById('submit');
        
        if(submit){
            if(ready){
                submit.setAttribute('aria-disabled', 'false');
                submit.removeEventListener('click', preventDefault);
            }else{
                submit.setAttribute('aria-disabled', 'true');
                submit.addEventListener('click', preventDefault(e));
            }
        }

    }

Submit buttons, when within a form, submit the form by default, so I figure using "preventDefault" on it is at least half right...?

How do I remove the listener when the form is ready so it returns to its default behavior? Is this correct?

I am not at a stage where I can adequately test this code yet (I am revising many things in this project and my submit buttons don't currently exist anymore), but I figured I would ask anyway since it seems like this is probably a basic enough question--I'm very much a beginner with this.

Also, in case it matters, "submit" is an <input type="submit"> element, not a <button>.

Thank you.
Was this page helpful?