sikas
sikas

Reputation: 5523

JQuery change new line to line break in TEXTAREA

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();
}

Upvotes: 4

Views: 3605

Answers (3)

dku.rajkumar
dku.rajkumar

Reputation: 18568

you can try

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

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

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

Upvotes: 4

glortho
glortho

Reputation: 13200

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 />');
    }
}

Upvotes: 4

techfoobar
techfoobar

Reputation: 66663

Change the assignment to oInput.val(temp); i.e.

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);
}

Upvotes: 0

Related Questions