samedi 14 septembre 2019

Make alert appear when start typing input

I am trying to make something happen only when a user inputs data into an input element that has been created. However I don't want to validate that data has been inputted or make the user press a button to check if data has been inputted, I want something to happen as soon as the first data value is typed in the input field. I decide to just use a demo of something similar that I want to create - to cut out the clutter:

I have tried:

var input = document.getElementById("input");

if(input == ""){
alert("no value");
}else{
input.style.background = "blue";
}
<input type="text" id="input">

But nothing seems to be working. For what reason is it not working? So in this example I would only want the background to be blue when the first data value is typed in. I also tried:

var input = document.getElementById("input");

if(input.length == 0){
alert("no value");
}else{
input.style.background = "blue";
}

and:

var input = document.getElementById("input");


if(input == undefined){
alert("no value");
}else{
input.style.background = "blue";
}

as well as variations using != and !==

Is it something small I'm missing?

Aucun commentaire:

Enregistrer un commentaire