How to Prevent User Hitting Enter Except Textarea using jQuery

AuthorSumit Dey Sarkar

Pubish Date11 Sep 2022

categoryJQuery

In this article we will learn how to prevent user hitting enter except textarea using jQuery.

Let's see example -

How to Prevent User Hitting Enter Except Textarea using jQuery

<!DOCTYPE html>
<html>

<head>
    <title>How to Prevent User Hitting Enter Except Textarea</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
</head>

<body>

    <h3>How To Prevent User Hitting Enter Except Textarea</h3>
    <form method="POST">
        <strong>Full Name:</strong>
        <input type="text" name="fullName" placeholder="Name">


        <strong>About Your Self:</strong>
        <textarea></textarea>


        <button type="submit">Submit Now</button>
    </form>


    <script type="text/javascript">
        $(document).ready(function () {
            $(window).keydown(function (event) {
                if ((event.keyCode == 13) && ($(event.target)[0] != $("textarea")[0])) {
                    event.preventDefault();
                    return false;
                }
            });
        });
    </script>


</body>

</html>

 

Output

jQuery

Comments 0

Leave a comment