Detect “Shift+Enter” And Generate New Line In JS

Hello, Hope you are doing well .!

In this article, We will learn the most common requirements in web applications that are based on JS. For example: How to detect shift + enter and generate a new line. So let’s start

 

Code:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<center>
  <h1 style="color:gray;">Detect “Shift+Enter”</h1>
  <h4>Press "enter" or "shift+enter" in the textarea, both does the same.</h4>
  <textarea rows="8" cols="50" onkeypress="detectShiftEnterFunc(event)"></textarea>
   	<p id="msg" style="color:red"></p>
</center>
</body>
<script>
  function detectShiftEnterFunc(event) {
    // 13 is the keycode for "enter"
    if (event.keyCode == 13 && event.shiftKey) {
      $('#msg').html("Triggered enter+shift");
    }
    if (event.keyCode == 13 && !event.shiftKey) {
      $('#msg').html("Triggered enter");
    }
  }
</script>
</html>

 

Output:

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories