How can I open a popup window using JQuery?

72.6k views Asked by At

I have this sample:

https://jsfiddle.net/bac8qdq1/

HTML:

<a id="OpenDialog" href="#">Click here to open dialog</a>
<div id="dialog" title="Dialog Title">
    <p>test</p>
</div>

JQuery:

$(document).ready(function () {
    $("#OpenDialog").click(function () {
        $("#dialog").dialog({
            modal: true,
            height: 590,
            width: 1005
        });
    });
});

I want that when a user clicks on the link to open, a new window pops up with a text area inside.

I tried the above code, but unfortunately it did not work ... Can you tell me how we should solve this problem? I want the window to open and contain a textarea element.

4

There are 4 answers

4
Md Johirul Islam On BEST ANSWER

Here is a solution. Yo can try

 $(document).ready(function () {
            $("#OpenDialog").click(function () {
                //$("#dialog").dialog({modal: true, height: 590, width: 1005 });
                var w = window.open("", "popupWindow", "width=600, height=400, scrollbars=yes");
                var $w = $(w.document.body);
                $w.html("<textarea></textarea>");
            });
        });

Here is the edited jsfiddle https://jsfiddle.net/bac8qdq1/13/

2
sgromskiy On

Or even without JavaScript. Just for fun.

#dialog{
  display: none;
}
#dialog:target{
  display: block;
}
#close{
  position: fixed;
  opacity: 0;
}
#close:target + #dialog{
  display: none;
}
0
Tushar On

To open dialog use open option:

$("#OpenDialog").click(function () {
    $(".selector").dialog("open");
});

Docs: http://api.jqueryui.com/dialog/#method-open

You can also use autoOpen option to open dialog on initialization:

$("#OpenDialog").click(function () {
    $("#dialog").dialog({
        modal: true,
        height: 590,
        width: 1005,
        autoOpen: true
        // ^^^^^^^^^^^
    });
});

Docs: http://api.jqueryui.com/dialog/#option-autoOpen

1
Maddy On

I have update the fiddle for you, please take a look. It is working

https://jsfiddle.net/bac8qdq1/12/

$(document).ready(function () {
    $("#dialog").dialog({ autoOpen: false, modal: true, height: 590, width: 1005 });

            $("#OpenDialog").click(function () {
                $("#dialog").dialog('open');
            });
        });