How to detect key pressed in Javascript inside textarea input?

Go To


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 breakings and I also don't want to use all other present texteditors 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.

2012-04-04 03:06
by monk
Are you in control of the backend - Ian Hunter 2012-04-04 03:08
yes, actually i want the text editor to be in the backend - monk 2012-04-04 03:15
I mean, do you have access to the code that saves the data in the database - Ian Hunter 2012-04-04 03:19
Yes. its my own page. I am developing it. I want my backend to be friendly to me - monk 2012-04-04 03:23


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

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

    // If the user has pressed enter
    if (key == 13) {
    return false;
    else {
    return true;
2012-04-04 03:17
by Madan


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.

2012-04-04 03:43
by Ian Hunter