jQuery keypress() method

Definition

Use the jQuery keypress() method to fire the keypress event i.e. when a keyboard key is pressed.

Syntax

The following is the syntax,

$(selector).keypress(func)

The following is the parameter for the keypress method,

  • func: When the keypress 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(){  
   $("#input").keypress(function(){
     $(this).css("background-color", "orange");
   });
});

</script>
</head>
<body>

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

<p>Add the details in the field above. The background color will change on pressing any key.</p>

</body>
</html>

The following is the output,

Press any key to change the background,

jQuery keydown() method
jQuery keyup() method
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment