Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an inverse function to jQuery serialize?

I can write something, I'm asking if something is already built into jQuery.

like image 539
garyM Avatar asked Mar 24 '12 23:03

garyM


People also ask

What is serialize function in 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.

What is the use of serialize ()?

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.

What is serialize array in jQuery?

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.

How do you serialize data in JavaScript?

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() .


2 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/

like image 159
Luke Avatar answered Oct 04 '22 03:10

Luke


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.

like image 24
gdoron is supporting Monica Avatar answered Oct 04 '22 01:10

gdoron is supporting Monica