简体   繁体   中英

How i can delete span text made in js?

How can I delete or hide the text while somebody in my form input in the second time give correct data? I mean when somebody on first time give incorrect the text will display "First Name cannot be empty." and then give correct data and the text is there but invisible.

        errorFunc(fname, 'First Name cannot be empty!')
    } else {
        successFunc(fname)
    }

    if (lastName === '') {
        errorFunc(lname, 'Last name cannot be empty!')
    } else {
        successFunc(lname)
    }

function errorFunc(req, message) {
    const formControl = req.parentElement;
    const span = formControl.querySelector('span');
    span.innerText = message;
    req.classList.add('error');
    span.classList.add('error-text');
    req.classList.remove('success');
}

function successFunc(req) {
    const formControl = req.parentElement;
    const span = formControl.querySelector('span');
    req.classList.add('success');
    req.classList.remove('error')
    span.classList.remove('error-text')
}

You have 2 options:

  • You can remove the span with.remove() function.

     span.remove()
  • Also you can maintain the span element but remove text.

     span.innerText = ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM