vir
vir

Reputation: 1091

How to reuse same jQuery ui dialog box code for different popup

I want to use the following jQuery dialog box for to open different popup box

<head runat="server">
    <title></title>
    <link href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#pop101").dialog({
                autoOpen: false,
                draggable: true,
                title: "Add New Person",
                open: function (type, data) {
                     $(this).parent().appendTo("form");
                }
            });
        });
        function showDialog(id) {
            $('#' + id).dialog("open");
        }
        function closeDialog(id) {
            $('#' + id).dialog("close");
        }
    </script>
</head>
<body>
    <input type="button" onclick="showDialog('pop101');" value="Popup1" />
    <input type="button" onclick="showDialog('pop102');" value="Popup2" />
    <input type="button" onclick="showDialog('pop103');" value="Popup3" />
    <input type="button" onclick="showpopup('pop104');" value="Popup4" />
    <div style="background: green" id='pop101'>
    </div>
    <div style="background: orange" id='pop102'>
    </div>
    <div style="background: blue" id='pop103'>
    </div>
    <div style="background: skyblue" id='dialog1'>
    </div>
</body>

There are 4 buttons popup1,2,3,4 , when i click on them, popup should display using above code, how?

Upvotes: 0

Views: 2282

Answers (1)

user1590083
user1590083

Reputation:

Try using a class for initializing the popup:

haven't tried yet but should work:

<head runat="server">
    <title></title>
    <link href="css/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".popup").dialog({
                autoOpen: false,
                draggable: true,
                title: "Add New Person",
                open: function (type, data) {
                    $(this).parent().appendTo("form");
                }
            });
        });
        function showDialog(id) {
            $('#' + id).dialog("open");
        }
        function closeDialog(id) {
            $('#' + id).dialog("close");
        }
    </script>
</head>
<body>
    <input type="button" onclick="showDialog('pop101');" value="Popup1" />
    <input type="button" onclick="showDialog('pop102');" value="Popup2" />
    <input type="button" onclick="showDialog('pop103');" value="Popup3" />
    <input type="button" onclick="showpopup('pop104');" value="Popup4" />
    <div style="background: green" id='pop101' class="popup">
    </div>
    <div style="background: orange" id='pop102' class="popup">
    </div>
    <div style="background: blue" id='pop103' class="popup">
    </div>
    <div style="background: skyblue" id='dialog1' class="popup">
    </div>
</body>

Here is the fiddle; it worked!!

Upvotes: 1

Related Questions