jQuery document handle key press event

Description

jQuery document handle key press event

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Check if Enter Key is Pressed with jQuery</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).on("keypress", function(e){
        if(e.which == 13){//from   www. j  ava 2s .c o m
            $("body").append("<p>You've pressed the enter key!</p>");
        }
    });
</script>
</head>
<body>
    <p>Click on the viewport and press the Enter key on the keyboard. 
    </p>
</body>
</html>



PreviousNext

Related