开发者

Javascript: How to retrieve a value from a popup after a click event is fired

开发者 https://www.devze.com 2023-02-21 17:22 出处:网络
I have a page where clicking on a link will open a popup window. The popup window will contain some value. Now when user clicks on a div tag, I want to copy the text of that div into a text box of the

I have a page where clicking on a link will open a popup window. The popup window will contain some value. Now when user clicks on a div tag, I want to copy the text of that div into a text box of the main window.

I have used following code to open a po开发者_如何学JAVApup -

popup = window.open(location, "popup","menubar=1,resizable=1,scrollbars=1,width=650,height=450");

How can I do that?


You can use opener to reference the parent window from your child window. Here's an example from W3 Schools...

... and here's a working example from me:

Parent Window

Script

    <script type="text/javascript">
        $().ready(function () {
                $('#link').click(function (e) { 
                    e.preventDefault();  
                    window.open("popup.htm"); 
                });
        });
        function updateContent(content) {
            $('#contentHolder').html(content);
        }
        </script>

HTML

<a id="link" href="popup.htm" target="_blank">Open Window</a>
<div id="contentHolder"></div>

Child Window

Script

<script type="text/javascript">
    $().ready(function () {
        $('#contentDiv').click(function () {
            window.opener.updateContent($(this).html()); 
        });
    });
    </script>

HTML

<div id="contentDiv">Clickable Div</div>


You tagged your question as jquery. There is one thing to look out for, if you want to use jquery inside the pop-up from the main window. It works something like this:

window.opener.$(selector, window.opener)

So, you are using the jquery of the pop-up and specify a context as well (2nd parameter). I am not completely sure, that 2nd step is necessary as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号