Search code examples
jqueryjsonjquery-nestable

How to return JSON as a string object using nestable plugin?


My code is used to make a nestable drag and drop menu, and after changes are made using the drag-and-drop functionallity the JSON is written to a textarea. How can I return this string value to a variable instead of pusing it into the textarea?

Html code:

<textarea id="nestable-output"></textarea>

The jquery code:

$(document).ready(function() {
    var updateOutput = function(e) {
        var list   = e.length ? e : $(e.target),
            output = list.data('output');   

        if (window.JSON) {
            output.val(window.JSON.stringify(list.nestable('serialize')));              
        } 
    };

    // activate Nestable for list 1
    $('#nestable').nestable({
        group: 1
    })
    .on('change', updateOutput);


    // output initial serialised data to textarea
    updateOutput(
        $('#nestable').data('output',
        $('#nestable-output'))
    );
});

Solution

  • Remove this:

    output.val(window.JSON.stringify(list.nestable('serialize')));
    

    Add this in its place:

    someGlobalVar = window.JSON.stringify(list.nestable('serialize'));
    

    And be sure to declare var someGlobalVar outside the function.