tonyf
tonyf

Reputation: 35587

Pass parameters into document.ready() calls

I am using a jQuery UI dialog but instead of duplicating the dialog setup 5 times, I was wondering if I can somehow pass the div id as a parameter into the document.ready() call or when I call the div in question?

For example>

$(document).ready(function(){ 

$(function() {
  location.hash = 'PAGETOP';
});

   $("#dialogou").dialog({
            autoOpen: false,
            closeOnEscape: false,
            resizable: false,
            modal: true,
            draggable: true,
            position:  ["center", 100],
            buttons: {
              'Ok': function() {               
                      $(this).dialog("close"); 
                      closeReq();
                    }
        }
    });  
 });

So based on the above, if I have the following if condition:

  if (document.getelementbyId("ERROR_OU").value == "Y")
    $('#dialogou').dialog('open');

I would like to be able to change the div id dilaogou to dialogao but still use the same call above but obviously swap out dilaogou to dilaogao.

Is this possible?

Upvotes: 0

Views: 296

Answers (2)

davidtbernal
davidtbernal

Reputation: 13694

How about using an array:

$(document).ready(function(){ 

var dialog_ids=['dialogou', 'dialaogao'];

$(function() {
  location.hash = 'PAGETOP';
});

for(id in dialog_ids)
{
       $("#"+dialog_ids[id]).dialog({
                autoOpen: false,
                closeOnEscape: false,
                resizable: false,
                modal: true,
                draggable: true,
                position:  ["center", 100],
                buttons: {
                  'Ok': function() {               
                          $(this).dialog("close"); 
                          closeReq();
                        }
            }
        });  
     });
 }

Upvotes: 0

Guffa
Guffa

Reputation: 700910

Just make a selector that matches any of the id:s when doing the initialization:

$("#dialogou, #dialogao, #dialogxx, #dialogyy, #dialogzz").dialog({
...

Now you have the dialog set up for all the elements, and you can call dialog('open') on any of them.

Upvotes: 2

Related Questions