JQuery: add line break at cursor position when hitting Enter - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:jQuery Method Example

Description

JQuery: add line break at cursor position when hitting Enter

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/*w  w  w  . jav a  2  s  .  co m*/
$('textarea').keypress(function (e){
    if(e.keyCode == 13){
        e.preventDefault();
        this.value = this.value.substring(0, this.selectionStart)+"<br />"+"\n";
    }
});
    });

      </script> 
 </head> 
 <body> 
  <textarea rows="4" cols="50"></textarea>  
 </body>
</html>

Related Tutorials