Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a value into a jQuery UI dialog box with a function

This is my document.ready code:

$(document).ready(function() {
  $("#dialogbox").dialog({
        open: function(event, ui) {$("a.ui-dialog-titlebar-close").remove();},
        bgiframe: true,autoOpen: false,closeOnEscape: false,draggable: false,
        show: "drop",hide: "drop",zIndex: 10000,modal: true,
        buttons: {'Ok': function() {$(this).dialog("close");processEmp();}}
  });
}); 

I have the following JavaScript code that takes one parameter:

function test(pEmp)
{ 
  var a = pEmp.value);

  $('#dialogbox').dialog('open');
}

My question is, based on the value that I pass into my test function, which in turn calls my jQuery UI dialog ('#dialogbox'), when the user presses the 'Ok' button in the dialog, I need to somehow (which is what I am not sure how to do), pass the the variable "a" which holds my pEmp.value, into my other function processEmp(a?), which I have attached to my 'Ok' button.

I basically need this value when the user acknowledges the dialog box.

like image 725
tonyf Avatar asked Nov 15 '11 14:11

tonyf


2 Answers

You may pass custom option to dialog before opening it:

$(function () {
     $("#dialog").dialog({
          open: function (event, ui) { $("a.ui-dialog-titlebar-close").remove(); },
          bgiframe: true,
          autoOpen: false,
          closeOnEscape: false,
          draggable: false,
          show: "drop",
          hide: "drop",
          zIndex: 10000,
          modal: true,
          buttons: { 'Ok': function () {
               $(this).dialog("close");
               processEmp($(this).data("pEmpValue"));
          } 
          }
     });
});

function processEmp(a) {
     alert(a);
}

function test(pEmp) {
     $("#dialog").data("pEmpValue", pEmp.value).dialog("open");
}

Or even the simplest solution is to declare a variable in scope of the window:

var a = null;

$(function () {
     $("#dialog").dialog({
          open: function (event, ui) { $("a.ui-dialog-titlebar-close").remove(); },
          bgiframe: true,
          autoOpen: false,
          closeOnEscape: false,
          draggable: false,
          show: "drop",
          hide: "drop",
          zIndex: 10000,
          modal: true,
          buttons: { 'Ok': function () {
               $(this).dialog("close");
               processEmp(a);
          } 
          }
     });
});

function processEmp(a) {
     alert(a);
}

function test(pEmp) {
     a = pEmp.value;
     $("#dialog").dialog("open"); 
}
like image 77
Yuriy Rozhovetskiy Avatar answered Nov 14 '22 23:11

Yuriy Rozhovetskiy


You can achieve this by adding an event handler for 'close'. Something like this:

$("#dialogbox").bind("dialogclose", function(event, ui) { processEmp(a); });
like image 41
Dmitry Golubets Avatar answered Nov 14 '22 22:11

Dmitry Golubets