简体   繁体   中英

JavaScript Change the text fields when the user fills or not when the text field loses focus

i need help with an assignment: When a user fills in a text field and the text field loses focus, make the text field green with a check mark. You can use a check mark from FontAwesome. When a user doesn't fill in a text field, but removes focus, make the text field red with a cross I need the javascript/dom code to complete this assignment. I have tried this code but doesnt work:

    let fields = document.getElementsByTagName("input");
const checkFields = function () {
    for (let i = 0; i < fields.length; i++) {
        if (fields[i].value == "") {
          fields[i].style.backgroundColor = "red";
        } else {
          fields[i].style.backgroundColor = "green";
        }
      }
}
fields.addEventListener('change', checkFields)



          <div class="controls">
            <input
              autofocus=""
              class="input-xlarge"
              id="company"
              name="company"
              placeholder="Company name"
              type="text"
              value=""
            />
          </div>
        </section>

        <section class="control-group">
          <label class="control-label" for="fname">Name</label>

          <div class="controls two-col">
            <input
              class="input-medium"
              id="fname"
              name="fname"
              placeholder="First name"
              type="text"
              value=""
            />
          </div>

          <div class="controls two-col">
            <input
              class="input-medium"
              id="lname"
              name="lname"
              placeholder="Last name"
              type="text"
              value=""
            />
          </div>
        </section>

        <section class="control-group">
          <label class="control-label" for="email">Email</label>

          <div class="controls">
            <input
              class="input-xlarge"
              id="email"
              name="email"
              placeholder="Email"
              type="email"
              value=""
            />
          </div>
        </section>

Here is your solution:

 // Select Each Input document.querySelectorAll('input').forEach((inp) => { // Remove whitespace from value and check it // Add eventlistener on each input inp.addEventListener('change', () => { let value = inp.value.split(' ').join('') // Do something with value; if (value == "") { inp.style.backgroundColor = "red"; } else { inp.style.backgroundColor = "green"; } }) })
 <section> <div class="controls"> <input autofocus="" class="input-xlarge" id="company" name="company" placeholder="Company name" type="text" value="" /> </div> </section> <section class="control-group"> <label class="control-label" for="fname">Name</label> <div class="controls two-col"> <input class="input-medium" id="fname" name="fname" placeholder="First name" type="text" value="" /> </div> <div class="controls two-col"> <input class="input-medium" id="lname" name="lname" placeholder="Last name" type="text" value="" /> </div> </section> <section class="control-group"> <label class="control-label" for="email">Email</label> <div class="controls"> <input class="input-xlarge" id="email" name="email" placeholder="Email" type="email" value="" /> </div> </section>

You can use "focusout" event which I believe will be a better choice if I understood your problem right.

const ips = document.querySelectorAll("input")

ips.forEach(ip=>ip.addEventListener("focusout", ()=>{
    const text = ip.value
    if (text === "") ip.style.border= "1px solid red"
    else ip.style.border = "1px solid green"
 })
)

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