monk
monk

Reputation: 4825

How to detect key pressed in Javascript inside textarea input?

I am trying to create a simple Javascript text editor that only makes paragraph breaks to entered paragraphs inside textarea input. while saving textarea input value in database they strip out the paragraph breaks and I also don't want to use all other present text editors because I only need the paragraph breaking (br tag) to be placed while hit enter key and should be saved like that with the tag inside the database. I could not find the solution by Googling.

Upvotes: 1

Views: 3784

Answers (2)

Ian Hunter
Ian Hunter

Reputation: 9774

Depending on your needs, you may consider using something to replace newlines with <br> tags on the server side rather than trying to insert the tags in the textarea itself. PHP has the nl2br function for example.

Upvotes: 0

Madan
Madan

Reputation: 690

<textarea id="txtArea" onkeypress="onKeyDown();"></textarea>

<script>
    function onKeyDown() {
    var key = window.event.keyCode;

    // If the user has pressed enter
    if (key == 13) {
       alert('enter');
    return false;
    }
    else {
    return true;
    }
}
</script>

Upvotes: 3

Related Questions