1

I'm having an issue when I serialize a form and I'm getting these little carriage arrows in the serialization of a textarea using jQuery's serializeArray() method

enter image description here

However, when I use a val() method, the result is like so:

enter image description here

Can anyone tell me why they are different? And how the heck do I get rid of the little carriage arrows and make it like the val() result, as the little arrow is causing me problems? (btw what is the proper term for the little arrow?)

kidcapital
  • 5,064
  • 9
  • 46
  • 68
  • If possible , can post `html` ? Is requirement to remove `newline` characters ? See http://stackoverflow.com/questions/4321041/javascript-newline-character Thanks – guest271314 Oct 22 '14 at 19:04
  • Is that `console.log` output? The first logging an object with properties, the second one logging a plain string? That could make all the difference. Try to click on the property to *expand* . – Bergi Apr 14 '15 at 21:55

2 Answers2

2

This is because serializeArray replaces carriage returns with "\r\n", and .val removes them in browsers that add them (IE and Opera).

https://github.com/jquery/jquery/blob/master/src/serialize.js

This leads to the somewhat weird result that:

$([some-selector]).val() != $([some-form]).serializeArray()[0].value

See this jsfiddle for an example:

https://jsfiddle.net/434sj450/

Here is an old jquery bug / explaination for this behavior:

http://bugs.jquery.com/ticket/6876

Lee
  • 2,610
  • 5
  • 29
  • 36
0

There is no difference in your results.

Your inspector just shows the linebreaks as carriage arrows, when inspecting an object like that. Try and double click the value, you properly see it expand to show the full string spread over 3 lines.

.val() returns the string value of the textarea.

.serializeArray() returns an array of elements in the form.

These two lines will output the same thing in the console:

console.log($(this).val());
console.log($('form').serializeArray()[0].value);
ostrgard
  • 380
  • 3
  • 9
  • I believe this is incorrect. `.serializeArray()` does not return the same values as `.val`. Short answer is `.serializeArray()` adds in proper carriage returns. See my answer for details. – Lee Apr 14 '15 at 21:45