skip to Main Content

i’m tring to create a multiple login form , every was fine but i want to disable next button if the email input is empty.
the input class="email"
and the button class ="btn-next"
i will appreciate any help thanks.

<sub>const pageBox = document.querySelector('.page-box');
const btnNext = document.querySelector('.btn-next');
const btnBack = document.querySelector('.btn-back');
const checkboxPass = document.querySelector('.checkbox-pass');
const passwordInput = document.querySelector('.password');
const loginTitle = document.querySelector('.loginTitle-text');
const userEmail = document.querySelector('.user-email');
const emailInput = document.querySelector('.email');

btnNext.onclick = (e) => {
    e.preventDefault();
    pageBox.classList.add('active-pass');
    setTimeout(() => passwordInput.focus(), 500);
    loginTitle.innerHTML = 'Welcome';
    userEmail.innerHTML = ''  + emailInput.value;
};

btnBack.onclick = (e) =>{
    e.preventDefault();
    pageBox.classList.remove('active-pass');
    loginTitle.innerHTML = 'Sign in';
    userEmail.innerHTML = 'Please continue,';
    emailInput.focus();
};

checkboxPass.onclick = () => {
    if(checkboxPass.checked) {
        passwordInput.type = 'text';
    }
    else{
       passwordInput.type = 'password';
    }
};
emailInput.addEventListener("btnNext", (e) => {
    preventDefault();
    btnNext.disabled()
    if(emailInput.value.length  > 0) {
        btnNext.disabled = 'true';
     }
     else{
        btnNext.disabled = 'false';
    } 
});
</sub>

2

Answers


  1. Instead of your current code,

    emailInput.addEventListener("btnNext", (e) => {
        preventDefault();
        btnNext.disabled()
        if(emailInput.value.length  > 0) {
            btnNext.disabled = 'true';
         }
         else{
            btnNext.disabled = 'false';
        } 
    });
    

    try to change to this code,

    emailInput.addEventListener("input", (e) => {
        e.preventDefault();
    
        if(emailInput.value.length  > 0) {
            btnNext.disabled = 'true';
         }
         else{
            btnNext.disabled = 'false';
        } 
    });
    
    Login or Signup to reply.
    1. There’s no btnNext event, use input (when the input is changed)
    2. Add or remove a disabled attribute for the button based on the input’s value.

    Setting disabled="false" doesn’t work as explained here.

    const btnNext = document.querySelector('.btn-next');
    const emailInput = document.querySelector('.email');
    
    emailInput.addEventListener("input", () => emailInput.value.length ? btnNext.removeAttribute('disabled') : btnNext.setAttribute('disabled', ''));
    <input type="email" class="email"/>
    <button class="btn-next" disabled>Next</button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search