jQuery keydown() method

Definition

Use the jQuery keydown() method to fire the keydown event i.e. an event gets fired when a keyboard key is pressed down.

Syntax

The following is the syntax,

$(selector).keydown(func)

The following is the parameter for the keydown method,

  • func: When the keydown event is fired, this function runs. This is an optional parameter.

Example

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

$(document).ready(function(){  
    $("#input1").keydown(function(){
      $(this).css("background-color", "gray");
    });
    $("#input2").keydown(function(){
      $(this).css("background-color", "orange");
    });
});

</script>
</head>
<body>

<p>Add you details here to attempt Studyopedia Quiz,</p>
Subject ID: <input id="input1" type="number"><br>
Subject Name: <input id="input2" type="text">

<p>Add the details in the fields above. The background color will change for both the fields.</p>

</body>
</html>

The following is the output,

On typing the values in the two input fields, the following is visible. The background color of the input fields will change,

jQuery hover() method
jQuery keypress() method
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment