Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add data to an already serialized array?

Tags:

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(); 
like image 730
NaN Avatar asked Dec 31 '12 14:12

NaN


1 Answers

.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); 
like image 190
Felix Kling Avatar answered Oct 28 '22 15:10

Felix Kling