jQuery keyup()

Introduction

Set the background color of an <input> field when a keyboard key is released:

Enter your name in the input field above.

It will change background color on keydown and keyup.

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/* ww w .  j  a v a 2 s .  co m*/
<script>
$(document).ready(function(){
  $("input").keydown(function(){
    $("input").css("background-color", "yellow");
  });
  $("input").keyup(function(){
    $("input").css("background-color", "pink");
  });
});
</script>
</head>
<body>

Enter your name: <input type="text">


</body>
</html>

The order of events related to the keyup event:

The keyup event occurs when a keyboard key is released.

The keyup() method triggers the keyup event, or attaches a function to run when a keyup event occurs.

Use the event.which property to return which key was pressed.

Trigger the keyup event for the selected elements:

$(selector).keyup()

Attach a function to the keyup event:

$(selector).keyup(function)
Parameter OptionalDescription
function Optional. function to run when the keyup event is triggered.



PreviousNext

Related