0

I have a quiz in a fancybox(2) iframe, when complete I have a score held in a hidden text box on the page within the Fancybox:

<form>
      <input type="text" id="theresult" hidden="true"/>
</form> 

Now when the user closes the Fancybox, I need to capture this value for the parent page to use, I have tried:

$('.fancybox1').fancybox({
        width: 800,
        height: 500,    
        beforeClose: function () {
                 var testResult = $("#fancybox-frame").contents().find('input#theresult');
                 $('#txtTestValue').attr('value', testResult );
        }
});

But this returns "[object Object]"...

What am I doing wrong?!

JFK
  • 40,963
  • 31
  • 133
  • 306
Digital Lightcraft
  • 455
  • 1
  • 7
  • 31

2 Answers2

2

Since you are using fancybox v2.x you may need to target the proper selector so try this

beforeClose : function () {
    var testResult = $(".fancybox-iframe").contents().find("#theresult").val();
    $('#txtTestValue').attr('value', testResult);
}

Notice the changes :

$("#fancybox-frame")

... should be

$(".fancybox-iframe")

... and I also added the .val() method.

JFK
  • 40,963
  • 31
  • 133
  • 306
  • If you want to learn a bit more how-to to pass variables back and forth between parent and iframed pages, you may want to check this http://stackoverflow.com/q/26426934/1055987 – JFK Oct 27 '14 at 16:26
  • You've hit it right on the head there JFK :-) Working nicely. Thank you! – Digital Lightcraft Oct 27 '14 at 16:28
1

You are storing the input field itself. You should be storing the value of the input:

var testResult = $("#fancybox-frame").contents().find('input#theresult').val();
ltalhouarne
  • 4,586
  • 2
  • 22
  • 31