Thursday, October 13, 2011

Passing data to jquery ui-dialog

I've always thought that passing of data to a dialog box in jquery is impossible. The only way to do this would mean setting a value to global variable and accessing it when ready. This post proves me wrong while browsing at stackoverflow.

jQuery provides a method which store data for you, no need to use a dummy attribute or to find workaround to your problem. Bind the click event:
$('a[href*=/Booking.aspx/Change]').bind('click', function(e) {
    e.preventDefault();
    $("#dialog-confirm")
        .data('link', this)  // The important part .data() method
        .dialog('open');
});
And your dialog:
$("#dialog-confirm").dialog({
    autoOpen: false,
    resizable: false,
    height:200,
    modal: true,
    buttons: {
        Cancel: function() {
            $(this).dialog('close');
        },
        'Delete': function() {
            $(this).dialog('close');
            var path = $(this).data('link').href; // Get the stored result
            $(location).attr('href', path);
        }
    }
});

1 comment: