Search code examples
jquerytextboxtextareakeycodeonkeypress

Hiding <br /> inside a text box, when the user presses enter key in jquery


This is similar to this question.

I am currenly trying to capture information froma user inside a textbox and then send this information in the exact format in an email to the owner of the page on submit.

At the moment the email that gets sent through appears on one line.I need to include the line breaks inside the email.

I have the following piece of code and I need to hide the line breaks from the user. Any suggestions would be greatly appreciated.

    $("textarea").keypress(function(e){
      if (e.keyCode == 13) {        
         $(this).val(  $(this).val() + "<br/>");
         //insert a newline
         $(this).val($(this).val() + "\n");
         alert("just enter was pressed");
         return;
      }
    }

Solution

  • Please try this: http://jsfiddle.net/yauqg/

    Hope it help the cause :)

    code

    $('#text').keyup(function(e){
          var keyed = $(this).val().replace(/\n/g,'<br />');
          $("#target").html(keyed);
     });​