clock  Mon - Sun 8.00 AM - 8.00 PM
fb
instagram
play store
pinterest

How to Prevent User Hitting Enter Except Textarea using jQuery

writter  Sumit Dey Sarkar
Date  11 Sep 2022
Language  JQuery
How to Prevent User Hitting Enter Except Textarea using jQuery

How to Prevent User Hitting Enter Except Textarea using jQuery

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="//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