简体   繁体   中英

How To Listen To All <input> Events and Then Execute A Command

I am creating a sign in / sign up form with several input fields. I want to know how to trigger a command whenever someone clicks or enters a value in any of the input field.

<form action="/" method="post">
    <div class="top-row">
        <div class="field-wrap">
            <label for="name">
                First Name
                <span class="req">*</span>
            </label>
            <input type="text" name="name" required autocomplete="off" />   
        </div>

        <div class="field-wrap">
            <label for="lastname">
                Last Name
                <span class="req">*</span>
            </label>
            <input type="text" name="lastname" required autocomplete="off"/>
        </div>
    </div>

    <div class="field-wrap">
        <label for="email">
            Email Address
            <span class="req">*</span>
        </label>
        <input type="email" name="email" required autocomplete="off" />
    </div>

    <div class="field-wrap">
        <label for="password">
            Password
            <span class="req">*</span>
        </label>
        <input type="password" name="password" required autocomplete="off" />
    </div>

    <button type="submit" class="button button-block">Get Started</button>
</form>

I want to be able to create some custom styling with the labels whenever someone tries to input something on the input field.

Here's the CSS I want to add to the tag whenever someone places some value in the tag:


label.active {
  transform: translateY(50px);
  left: 2px;
  font-size: 14px;
}

label.highlight {
  color: #fff;
}

Here's my non-working Javascript code:

const inputs = document.querySelectorAll('input');

inputs.addEventListener('keyup focus blur', ()=> {
    if(inputs.length > 0) {
        inputs.previousElementSibling.classList.add('active highlight')
    } else {
        inputs.previousElementSibling.classList.remove('active highlight')

    }
})

You mean this?

Change the label class if field not empty

 function listen(e) { var tgt = e.target; if (tgt.tagName === "INPUT") { tgt.previousElementSibling.classList.toggle('active', tgt.value.trim() !== ""); } } document.getElementById("myForm").addEventListener("input", listen) document.getElementById("myForm").addEventListener("focus", listen) document.getElementById("myForm").addEventListener("blur", listen) 
 label.active { transform: translateY(50px); left: 2px; font-size: 14px; color:green; } 
 <form action="/" method="post" id="myForm"> <div class="top-row"> <div class="field-wrap"> <label for="name">First Name <span class="req">*</span></label> <input type="text" name="name" required autocomplete="off" /> </div> <div class="field-wrap"> <label for="lastname">Last Name <span class="req">*</span></label> <input type="text" name="lastname" required autocomplete="off" /> </div> </div> <div class="field-wrap"> <label for="email">Email Address <span class="req">*</span></label> <input type="email" name="email" required autocomplete="off" /> </div> <div class="field-wrap"> <label for="password">Password <span class="req">*</span></label> <input type="password" name="password" required autocomplete="off" /> </div> <button type="submit" class="button button-block">Get Started</button> </form> 

You can loop through the elements and attach an event listener to each of the inputs or you can use event delegation on the container as detailed here:

addEventListener to ALL INPUT elements when DOM changes

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