Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery change new line to line break in TEXTAREA

Tags:

jquery

I've been trying to change the new lines to line breaks in the <TEXTAREA> but I'm failing to do so ...

I want to do this cause I take the value and append it to another <TEXTAREA> that is copied later on to the clipboard. When I copy to the clipboard, everything is set to a single line due to the new line in the <textarea>

Here is my code:

if(oInput.get(0).tagName == "TEXTAREA")
{
    temp = oInput.val();
    temp = temp.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '<br />');
oInput.val() = temp.val();
}
like image 645
sikas Avatar asked Oct 23 '25 16:10

sikas


2 Answers

Try this:

if(oInput.get(0).tagName == "TEXTAREA") {
    oInput.val(function(index, value) {
        return value.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '<br />');
    }
}
like image 95
glortho Avatar answered Oct 26 '25 08:10

glortho


you can try

temp.replace(/\n/g,'<br/>');

second thing is, change oInput.val(temp);

fiddle : http://jsfiddle.net/yXhUV/

like image 28
dku.rajkumar Avatar answered Oct 26 '25 08:10

dku.rajkumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!