How to access jQuery serialized data?

laukok picture laukok · Oct 20, 2013 · Viewed 68.5k times · Source

How can I access the data that has been 'serialized' using the jQuery function?

var test = $("form").serialize();

I have this data...

url=hello+1&title=hello+1&content=abc

How can I get the value of 'url'? - I tried this...

console.log(test.url); 

But I get undefined - I want to get the result as "hello+1"

Answer

Tsanyo Tsanev picture Tsanyo Tsanev · Oct 20, 2013

The serialize method only creates a URL encoded string from a form, but this is a string, i.e you can not get the value of url or any other value. To do so you need to parse back the string into an object. You can check library out: https://github.com/kflorence/jquery-deserialize

However it would be best if you simply select the field containing that value and get it from there, i.e.

jQuery('input[name="url"]').val();

or if you have an id or class on that field, you can use it as a selector.

P.S. The value of url is "hello 1" the + is just how spaces are encoded in query strings.