1

I've have created a simpleModal popup. I want to return a value from a text input on the popup to a asp textbox on the parent page.

I have read all of the examples that are similar to this one, but none have answered my question.

My popup is a simple aspx page with a text input. Here is the code:

JavaScript

  $(document).ready(function() {
    $("a#lookup").click(function () {
      //Load the Lookup page as a modal popup..
      $.modal('<iframe src="TestPopup.aspx" height="200" width="450" style="border:5">', {
        onOpen: function (dialog) {
            dialog.overlay.fadeIn('slow', function () {
                dialog.container.slideDown('100', function () {
                    dialog.data.fadeIn('fast');
            }); 
          }); 
        },
        preventDefault: true,
        containerCss: {
          backgroundColor: "#fff",
          borderColor: "#aaa",
          height: 530,
          padding: 5,
          width: 880,
          escClose: false
        },
        closeHTML: "<a href='#'>Close</a>",
        appendTo: 'form',
        persist: true,
        overlayClose: true,
        onClose: function (dialog) {
          var result = dialog.data.find("#msgText").val();
          $('#txtLookupReturn').attr('value', result);
          dialog.data.fadeOut('200', function () {
            dialog.container.slideUp('200', function () {
              dialog.overlay.fadeOut('200', function () {
                $.modal.close();
              }); 
            });
          });
        }
      }); 
    });    
  });   

TestPopup.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPopup.aspx.cs" Inherits="WebServicesTest.TestPopup" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">; 
<html xmlns="w3.org/1999/xhtml">; 
<head runat="server"> 
  <title></title> 
</head> 
<body> 
  <form id="form1" runat="server"> 
  <div> 
    <input id="msgText" type="text" value="I love jQuery!" /> 
  </div> 
  </form> 
</body> 
</html> 

This is the line of code that is not working:

var result = dialog.data.find("#msgText").val();

$('#txtLookupReturn').attr('value', result);

When I try to grab the value of #msgText, I get no error and no value. Appreciate any help!

RustyTheBoyRobot
  • 5,790
  • 4
  • 35
  • 54
Stephen Lerch
  • 41
  • 2
  • 5

1 Answers1

2

I think that your selection code should be this:

$("iframe[src='TestPopup.aspx']").contents().find("#msgText").val();

Selecting elements inside of iframes has some complexities which are addressed in this question and this blog post. This question talks about permissions with JavaScript and iframes. I can't tell how your pages are organized, so I don't know if you will have problems with the same origin policy, but it's a good thing to read up on anyways.

Update

The more I look at your popup HTML, the more I think that you don't need to use an iframe. When I've used SimpleModal, I usually put my dialog HTML in an invisible <div>. The iframe will be unnecessarily slow and increase your server traffic. If you need to load values from the server, you might want to keep it as an iframe. If, however, you just need the user to provide values, include the dialog form as HTML on your page.

Community
  • 1
  • 1
RustyTheBoyRobot
  • 5,790
  • 4
  • 35
  • 54