Generic jqueryui dialog

Viewed 30

I have a number of places in my website where I have a jqueryui dialog. In many cases the jquery code is almost identical. Typical differences are for title, width, and height.

The dialog is attached to a control with something like $('#selector').dialog().

Is there a way to make this like a "function that is called with parameters"? That is, have a generic jquery function instead of have numerous nearly identical codes?

1 Answers

You can try below code for generic function for dialog. Please upvote the answer if you find this useful. Thanks.

<button id="btn1" onclick="GenericDialog('Title 1',300,250)">Open the dialog</button>
<button id="btn2" onclick="GenericDialog('Title 2',200,150)">Open the dialog</button>
<div id="dia">
<p>Some txt goes here</p>
</div>

$('#dia').dialog({
    autoOpen: false,
    title: 'Basic Dialog'
});

function GenericDialog(title,width,height){
    $("#dia").dialog({ 
        width: width, 
        height: height, 
        title: title})
    .dialog('open');
}
Related