-1

Why \n is appending to the value after JSON.stringify. Here is the example link

http://jsfiddle.net/7nketLmy/

 var formData = new Array();
 if ($(this).get(0).tagName.toUpperCase() === 'DIV' ){
        content = $(this).html();
    } 
 alert(JSON.stringify(formData));

Note: Content in the Div is dynamic and I don't have any control on what is displaying there. I should use .html() to get the data in Div

user2901901
  • 101
  • 1
  • 12
  • 1
    In your fiddle, do you see the spaces between the open `
    ` the text and the closing `
    ` you see you added new line right that why it's adding \n to indicate that new line in the text format
    – andrex Sep 23 '14 at 05:50
  • Because there is `\n` in `
    ` html.
    – Regent Sep 23 '14 at 05:50
  • If you change you fiddle div tag to like this `
    Empire City Casino at Yonkers Raceway
    ` which has no spaces then there is not \n being added. It's because the string being retrieve has newlines but HTML doesn't interpret new lines so you can't see it in the browser.
    – andrex Sep 23 '14 at 05:51
  • And [updated fiddle](http://jsfiddle.net/7nketLmy/6/) about it. – Regent Sep 23 '14 at 05:52
  • If you have to no control over the content since it's dynamic you can trim it content.trim() – andrex Sep 23 '14 at 05:58

4 Answers4

1

change this part to your code: formData.push($.trim(content));

Raymond Cheng
  • 2,425
  • 21
  • 34
1

You added a new line in your div. that's why /n appearing

<div contenteditable="true" placeholder=""  name="content[0]" id="pres_preview_58c57bd0044aa58704f13133b381e97a_0" class="pdfElement tempContent txtfield">Empire City Casino at Yonkers Raceway</div>

Use like this.

Prateek
  • 6,785
  • 2
  • 24
  • 37
0

You added a new line and lots of spaces in your div in html, removing them will fix the issue:

<form name="presentationForm" id="presentationForm" method="post">
    <div contenteditable="true" placeholder=""  name="content[0]" id="pres_preview_58c57bd0044aa58704f13133b381e97a_0" class="pdfElement tempContent txtfield">Empire City Casino at Yonkers Raceway</div>       
</form>       
<input type="button" name="done" value="Done" onClick="javascript: saveTempInfo()">

Updated Fiddle

Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
0

Because there is \n in div tag

Check this fiddle: http://jsfiddle.net/7nketLmy/2/

   <div contenteditable="true" placeholder=""  name="content[0]"  id="pres_preview_58c57bd0044aa58704f13133b381e97a_0" class="pdfElement tempContent txtfield">empire City Casino at Yonkers Raceway</div>
Swetha
  • 746
  • 10
  • 19