jQuery::Send data to a dialog

A simple example regarding how to send data to a new elements. This is useful if you don’t want to use default jquery ui buttons on dialog

$('#open_dialog').click(function() {
    var jsonText = '';
    // create json string
    var obj = new Object();
    obj.mode  = "Update";
    obj.id    = id;
    obj.title = title;
    obj.toJSON = function(key) {
        var replacement = new Object();
        for (var val in this) {
            replacement[val] = this[val];
        }
        return replacement;
    };
    jsonText = JSON.stringify(obj);
    // pass it through data to dialog
    $('#my_dialog').data('mydata', jsonText).dialog('open');
});

// if dialog button clicked
$('#my_dialog_update').click(function() {
    var jsonText = $('#my_dialog').data('mydata');
    var arr = jQuery.parseJSON(jsonText);
    if (arr.id !== '') {
        if (arr.mode == "Update") {
            // send id and title and do update work
        }
    }
});