2

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;
      }
    }
Community
  • 1
  • 1
BridC
  • 23
  • 4
  • 1
    First, textarea elements only hold text, not child html elements (so no `
    ` elements). Second, inserting `
    ` at the _end_ of the current value doesn't make sense because the user isn't necessarily typing at the end of the field. You should do any processing like this on submit, preferably on the server-side if that's where the email is sent from - can't you take the completed value at that point and replace `\n` with `
    ` (assuming the emails you're sending are actually in html format)?
    – nnnnnn Jul 23 '12 at 02:25
  • Thanks for the info. I'm using the textarea for feedback purposes from users. At the moment it's just showing everything on one line and is stripping teh formatting as it's not recognising the html. Any ideas as to where I'm going wrong also still a newbie...Thanks in advance. – BridC Jul 23 '12 at 02:46
  • How do you generate the email? Server-side? – nnnnnn Jul 23 '12 at 03:10

1 Answers1

1

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);
 });​
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • 1
    You can omit the `[]` brackets from that regex. This same processing should also be done on blur or submit (to allow for the user drag'n'dropping or pasting text into the field). – nnnnnn Jul 23 '12 at 02:34
  • Thank-you bruv done! @nnnnnn (what does `=>` **nnnnnn** mean - if I may ask) :) – Tats_innit Jul 23 '12 at 02:35
  • Thanks for the tip Tats_innit I had just solved it slightly differently. The problem I am having is that it is not recognising teh html tags and strips out the formatting. – BridC Jul 23 '12 at 02:36
  • Cheers Bro also I'm a female :)This is what I have , also I'm still quite new to jquery and trying master it: $("textarea").keypress(function(e){ if (e.keyCode == 13) { var keyed = $(this).val().replace("/\n/g",'
    '); $("textarea").html(keyed); } });
    – BridC Jul 23 '12 at 02:39
  • @BridC lol Yo Cuz, saweet! :P srry for bruv - Ah cool, nice one indeed bird! +1 – Tats_innit Jul 23 '12 at 02:49
  • At the moment it's just showing everything on one line and is stripping the formatting as it's not recognising the html. – BridC Jul 23 '12 at 02:53
  • @BridC Cooleos, flick me the issue in jsfiddle i.e. re-use my demo to replicate it and I should be able to help you out – Tats_innit Jul 23 '12 at 02:54
  • What does "nnnnnn" mean? My real first name has six letters and starts and ends with "n" - I first used "nnnnnn" on another forum because another user had already registered with my first name, and I've since used "nnnnnn" in other forums including SO. @BridC - Don't put the result back into the textarea, use it directly to create the email (if you have JS code doing that) or put it in a hidden field to submit to the web-server, or the best option is not to do it in JS at all and do it all on the web-server if that's where you're creating the email. – nnnnnn Jul 23 '12 at 03:10
  • @nnnnnn ummm = `NathaN` ?? :) c'mon tell me I guessed it right! :P – Tats_innit Jul 23 '12 at 03:20
  • The problem is that I'm using oracle idoc script and xml escaping the text to prevent the user from entering random tags or anything else not suitable. – BridC Jul 23 '12 at 03:56
  • Thanks guys for all your assistance it's basically working now just need do some testing. $(document).ready(function(){ $("button[name=submit]").click(function(){ $("textarea[name=text]").val( $("textarea[name=text]").val().replace(/\n/g, '
    ') ); console.log($("textarea[name=text]").val()); }); }); My comment was being xml escaped took that off and it worked ok for now at least.
    – BridC Jul 23 '12 at 21:51
  • Could any of you recommend a good resource for learning jquery properly. I've jut been googling frantically and looking at jquery.com – BridC Jul 23 '12 at 23:51
  • @BridC lol here you go: http://jquery.com/ also you need to take time and let the knowledge sync in `:)` are you from victoria uni? :P anyhoo take a deep breath and try to understand the use and basics - I bet you will be rocking! – Tats_innit Jul 23 '12 at 23:53
  • @nnnnn : took your advice and used hidden element works like a charm. Sorry didn't quite understand yesterday been working on it all day. – BridC Jul 24 '12 at 00:00