22

I am using CKEditor and would like to serialize the textarea data along with all of the other elements. Is this possible?

I would like to append the taData to vals if possible.

var vals = $("#post").find('input,select').serialize();
var taData = CKEDITOR.instances.ta1.getData();
greybeard
  • 2,249
  • 8
  • 30
  • 66
NaN
  • 1,286
  • 2
  • 16
  • 29
  • 1
    Well... `.serialize` returns a string. You can always modify the string if that is what you want to know. But maybe you are more interesting in `.serializeArray()`: http://api.jquery.com/serializeArray/. – Felix Kling Dec 31 '12 at 14:27
  • Thanks Felix. Can you please show me how to do that? I'm JQ illiterate. – NaN Dec 31 '12 at 14:28
  • well, serialize main motto was to get values from whole the form and make it string! e.g. ?name=user&password=heart – Muhammad Talha Akbar Dec 31 '12 at 14:29
  • OK, so then I really can't append the textarea data to the 'vals' string, right? – NaN Dec 31 '12 at 14:30
  • get the textarea value using val() function – Muhammad Talha Akbar Dec 31 '12 at 14:31
  • @AspiringAqib. It won't work that way. I have to use CKs getData method to do that. I've already tried. – NaN Dec 31 '12 at 14:32
  • see this : http://stackoverflow.com/questions/14062654/ckeditor-getdata-doesnt-seem-to-work-as-it-should – Muhammad Talha Akbar Dec 31 '12 at 14:35
  • i am concerned with jQuery only and not with CKeditor and i don't know what is getData :D – Muhammad Talha Akbar Dec 31 '12 at 14:36
  • Have a look here: http://stackoverflow.com/questions/6627936/jquery-post-with-serialize-and-extra-data. – Felix Kling Dec 31 '12 at 14:37
  • Thank you Felix, this seems to be what I need. – NaN Dec 31 '12 at 14:38
  • If you don't send the data via any of the Ajax methods, you can serialise the array later using `$.param`: http://api.jquery.com/jQuery.param/. – Felix Kling Dec 31 '12 at 14:40
  • Thanks Felix. Using the post you linked to, what I get is a bunch of `[object Object]`. Is there a way for me to change that back into a query string? – NaN Dec 31 '12 at 14:44
  • I wrote a proper answer... don't use `alert` to inspect variables. `[object Object]` is the default string representation of objects. Rather use `console.log` and yes, you can use `$.param` to create a string, just as `.serialize` does. – Felix Kling Dec 31 '12 at 14:46

1 Answers1

47

.serialize returns a string, so you can always modify the string, but I would not recommend this, string manipulation can get messy.

Instead, use .serializeArray [docs] to create an array representation of the data and then add the data to it. Each element of the array is an object with a name and value property:

var vals = $("#post").find('input,select').serializeArray();
vals.push({name: 'nameOfTextarea', value: CKEDITOR.instances.ta1.getData()});

All jQuery Ajax methods will understand this structure and serialize the data properly. In case you want to create a serialized string (just like .serialize), you can pass the array to $.param [docs]:

var query_string = $.param(vals);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143