user14063075
user14063075

Reputation:

How to change the color of a button when the input feild is filled

I've search the internet and i have not seen anybody talk about how to change the color of a button when the one input bar is filled in javascript, i dont have any code i've really tryed since there little to any sources i've seen that talk about this in in-depth.

for example lets say the color of the button is white, but when i input any form of text, it changes colors to green. Heres a screenshot if that helps, sorry for the horrible wording.enter image description here

Upvotes: 0

Views: 86

Answers (1)

Parth B Thakkar
Parth B Thakkar

Reputation: 115

I understand you want to change the color of the button when text box is filled.

You can use this method.

You can use onKeyUp event or onChange event to perform that kind of requirement.

function changeButtonColor() {
  if(document.getElementById("myText").value !== "") {
     document.getElementById("myButton").style.background = "red";
  } else {
     document.getElementById("myButton").style.background = "blue";
  }
}
<html>
    <body>
        <input type="text" id="myText" onkeyup="changeButtonColor()">
        <br><br>
        <button id="myButton" style="background:blue">Click Me</button>
    </body>
</html>

Upvotes: 3

Related Questions