I can write something, I'm asking if something is already built into jQuery.
jQuery serialize() Method The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.
Definition and Usage The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network.
The serializeArray() is an inbuilt method in jQuery which is used to create a JavaScript array of objects that is ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls. The controls can be of several types.
In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With