I can write something, I'm asking if something is already built into jQuery.
-
1There might be, but it depends on the context of your problem, which you haven't told us. What are you trying to do? – Bojangles Mar 24 '12 at 23:29
-
Maybe this helps: http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript – stewe Mar 24 '12 at 23:30
-
Thanks I was just trying to be lazy.. I just never understood hwy make a convenient functions and not provide the inverse.. cheers :) – garyM Mar 25 '12 at 00:10
3 Answers
Short Answer: No, there isn't something built into jQuery to do this. Not sure why not...
But here is a jQuery plugin that should do the trick if you are using .serialize()
to get a serialized string:
$.fn.deserialize = function (serializedString)
{
var $form = $(this);
$form[0].reset(); // (A) optional
serializedString = serializedString.replace(/\+/g, '%20'); // (B)
var formFieldArray = serializedString.split("&");
// Loop over all name-value pairs
$.each(formFieldArray, function(i, pair){
var nameValue = pair.split("=");
var name = decodeURIComponent(nameValue[0]); // (C)
var value = decodeURIComponent(nameValue[1]);
// Find one or more fields
var $field = $form.find('[name=' + name + ']');
// Checkboxes and Radio types need to be handled differently
if ($field[0].type == "radio" || $field[0].type == "checkbox")
{
var $fieldWithValue = $field.filter('[value="' + value + '"]');
var isFound = ($fieldWithValue.length > 0);
// Special case if the value is not defined; value will be "on"
if (!isFound && value == "on") {
$field.first().prop("checked", true);
} else {
$fieldWithValue.prop("checked", isFound);
}
} else { // input, textarea
$field.val(value);
}
});
return this;
}
(A) The reset at the top is optional. You may want to reset the field values first, or may want to clear them using something like this: https://stackoverflow.com/a/6786237/1766230
(B) See https://stackoverflow.com/a/24417399/1766230 for why we need to replace +
.
(C) See Mozilla Developer Javascript Reference for info on decodeURIComponent
.
(Note: If you're using .serializeArray()
or some other way to serialize your data, the above won't work; you may find some of these solutions useful: jQuery plugin to serialize a form and also restore/populate the form?)
Boring usage example:
var $form = $('form.myFormClass');
var s = $form.serialize();
$form.deserialize(s);
Here's a jsfiddle that let's you play around with setting values, clearing, resetting, and "deserializing": http://jsfiddle.net/luken/4VVs3/
-
1
-
1This fails on inputs with square brackets in the name attribute like ``. Use `'[name="' + name + '"]'`instead, to support inputs like this. – frzsombor Jun 24 '19 at 10:03
No there is not.
How would you know which DOM element to change? There could be the same elements with the same name in different forms etc'.
Maybe parseJSON will help you:
jQuery.parseJSON(json) Returns: Object
Description: Takes a well-formed JSON string and returns the resulting JavaScript object.

- 147,333
- 58
- 291
- 367
-
Thanks... jQuery.serialize uses the name attributes to serialize the element and the value. That is besides the URI encoding and packing. The process reverses fairly easily. I'm new to javascript ad jQuery, I just thought I was missing something in the doc. I'll have to bite the bullet and move to json – garyM Mar 25 '12 at 00:07
-
5_"No there is not, and can't be"_ - sure there could be. Just call the (proposed) `deserialize()` method on a particular form and the values would go back to that form. If there's more than one element with the same name these should be set in order from the duplicates in the serialised version (the reverse of what `.serialize()` does). I've successfully used such a function on multiple projects (not with jQuery), but it is up to the programmer to make sure the selectors are right... – nnnnnn Mar 25 '12 at 00:34
-
@nnnnnn. You're right, there could be an unsafe version of it. But jQuery (as much as I am aware of)has zero unsafe functions. He can write the first one if he like to... – gdoron Mar 25 '12 at 05:40
Here is :
a=$('.price_analysis input').serializeArray();
aa={};
for (var i in a) {
aa[a[i].name] = a[i].value;
}
alert( JSON.stringify(aa));

- 240
- 3
- 4