0

I have a textarea on a page that I update and set the data in using ajax, but for some reason line breaks are represented by a \n. I've tried server side changing them and client side but no matter what I do I either get a \n in the textarea or a <br /> tag in there.

I have tried using .val(), .text() and .html() to get and set the info.

Any help really appreciated

Thanks

Sorry the code I am using

to update using ajax

var genNotes = $('#MainContent_TxtGenNotes').val();
var param = { notes: genNotes } 
 $.ajax({
    url: "default.aspx/UpdateInfo",
    data: JSON.stringify(param),
    dataType: "json",
    type: "POST",
    etc etc

the code I'm using to retrieve

 $.ajax({
    url: "default.aspx/UserDetails",
    data: JSON.stringify(param),
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataFilter: function (data) { return data; },
    success: function (data) {
        try {
        var json = JSON.stringify(data);
        var json_obj = $.parseJSON(json);
        $('#MainContent_TxtGenNotes').html(json_obj[0].genNotes);
        etc etc
3
  • 2
    Can you show us the code you have tried? Commented Feb 6, 2015 at 10:29
  • As well as the code, seeing your data you're trying to set as the textarea value would help - I'm guessing it's encoded somehow. Commented Feb 6, 2015 at 10:31
  • Use .val(), then make sure all \n only have a single backward slash. You can use replace to replace all \\nor\\\n with just a single slash. Commented Feb 6, 2015 at 10:33

2 Answers 2

1

It might be due to the character escaping in the ajax process. Make sure you replace \\n with \n from your ajax response.

You may try ajaxResponse.replace(/\\+n/g,"\n");

Sign up to request clarification or add additional context in comments.

1 Comment

I think your absolutely right with that. That seems to have done it.
1

You should set the data using .html().

eg.

<textarea id="area1"></textarea>

$("#area1").html("1\n2");

JS Fiddle

3 Comments

Yes I can read. But using .html() is the way to achieve this.
So if it didn't work for the OP, why is it now suddenly going to work?
Well I suppose without seeing his code we'll never know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.