简体   繁体   中英

How can i change the class of a div when input field has a specific value?

I want to change the background of a div when an input field gets a specific value. eg:

<div class="vote-container">
<input class="positive" title="" name="pvote_1" 
onclick="" value="+"  type="submit">
</div>

So when the input field gets the "+" value i need to add the class "green" to the div "vote-container" and become like this:

<div class="vote-container green">
<input class="positive" title="" name="pvote_1" 
onclick="" value="+"  type="submit">
</div>

This will do it.

    <div class="vote-container">
    <input class="positive" title="" name="pvote_1" 
    onclick="" value="+"  type="submit">
    </div>

    <script>
    var input = document.querySelector(".positive");
    if(input.value == "+"){
        document.querySelector(".vote-container").classList.add("green");
    }
    </script>

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