How can I add paragraphs to a TEXTAREA TAG?

Asked

Viewed 157 times

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');
        }                   
    });
});

1 answer

1


Basically 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>
<textarea id="textarea"></textarea>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.