Sunny Basra
Sunny Basra

Reputation: 21

I want users to see a line break in my <textarea> instead of a <br>

When I type br into my <textarea> I really mean:

<br>

When key 13 (return) is pressed, I insert br into the .message <textarea>. This puts a nice line break in my <div> with the class .M.

My problem is that the user sees br in the .message <textarea>. I have to put it in there else the br wont be added next time a key is pressed.

Here's my code:

$('.message').keyup(function(e){
        pos = $('.message').prop("selectionStart");
        start = $('.message').val().substring(0,pos);
        end = $('.message').val().substring(pos);
    //preventers
    if(e.keyCode==222){$('.message').val(msg);$('.M').html(msg);}else{msg = $('.message').val();$('.M').html(msg);}
    //replacers
        if(e.keyCode==13){msg = start+'<br>'+end;$('.message').val(msg);$('.M').html(msg);}else{msg = $('.message').val();$('.M').html(msg);}
    //sweep
    pattern=/'/gi;var illegal=msg.match(pattern);
    if(illegal!==null){illegal=illegal.length;while(illegal>0){msg=msg.replace("'","");msg=msg.replace('"','');$('.M').html(msg);$('.message').val(msg);illegal--;}}
    pattern=/"/gi;var illegal=msg.match(pattern);
    if(illegal!==null){illegal=illegal.length;while(illegal>0){msg=msg.replace("'","");msg=msg.replace('"','');$('.M').html(msg);$('.message').val(msg);illegal--;}}
});

HTML:

<textarea class="message" id="message" value="" maxlength="75"
         style="position:relative;top:1p‌​x;left:5px;resize:none;border:none;background:none;height:17px;width:428px;">
</te‌​xtarea>


<div class="M_" 
     style="position:relative;top:100px;left:0px;height:160px;width:200px;c‌​olor:#000;z-index:2;">
</div> 

Upvotes: 2

Views: 221

Answers (1)

Hogan
Hogan

Reputation: 70513

I think you might reconsider when you perform the replacements. If you try to do this interactively you are going to have a hard time. Far better to wait for the submit of the data and only change the data that is being sent to the server.

Upvotes: 1

Related Questions