How can I add paragraphs in a TEXTAREA TAG?

1
$("#text").on("focus", function(){
    $(this).keypress(function(e){                   
        if(e.keyCode == 13 | e.which == 13){
            //this.value += '</p><p>';
            document.execCommand('formatBlock', false, 'p');
        }                   
    });
});
    
asked by anonymous 13.04.2017 / 14:20

1 answer

1

Basically you only need to validate when typed Tab and add a tab ( \t ).

$(document).delegate('#textarea', 'keydown', function(e) {
  var keyCode = e.keyCode || e.which;

  if (keyCode == 9) {
    e.preventDefault();
    var start = this.selectionStart;
    var end = this.selectionEnd;

    $(this).val($(this).val().substring(0, start) + "\t" +
    $(this).val().substring(end));

    this.selectionStart =
    this.selectionEnd = start + 1;
  }
});
#textarea {
  width: 500px;
  height: 200px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><textareaid="textarea"></textarea>
    
13.04.2017 / 15:36