Trigger function after change in input without Submit

Asked

Viewed 96 times

-2

Good morning/ Good afternoon/ Good evening

Here is my problem: I need that as the user goes typing in the field my function is triggered, without need to give a Submit. I’m trying to use the change event, only it only triggers the function after the change of focus of that field.

follows what I’m trying to do:

form.addEventListener('change', function(e) {...}

So I’d like to know if there’s a specific event for that that I want,.

Grateful!

  • 1

    Hello, You can use the keyup events (triggered immediately after the user takes his finger off the pressed key), or keydown (triggered ineditely after the user enters something)

  • " as the user enters " if the user is typing must be an input, textarea, etc... pq is adding the event to the form? should be in the element change

2 answers

0

You can use the event onkeyPress('Calls function when pressing qlq key') or onkeydown('Calls function on release key pressed') Below is an example:

<!DOCTYPE html>
<html>
<body>

<input type="text" id="demo" onkeydown="keydownFunction()" onkeyup="keyupFunction()">

<script>
function keydownFunction() {
  document.getElementById("demo").style.backgroundColor = "red";
}

function keyupFunction() {
  document.getElementById("demo").style.backgroundColor = "green";
}
</script>

</body>
</html>

0

I did a function similar to the answer above, Follow the codic:

<DOCTYPE HTML>

<html>

<body onload="alerta()">

 <input type="text" id="campo" placeholder="Insira um valor aqui">

 <script>

   function alerta() {

     alert("Funçao executada com sucesso!")

     document.getElementById("campo").addEventListener("keydown", alerta)

    }

 </script>

</body>

</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.