Monday, February 21, 2011

Get radio value inside iframe

How do I go about doing this with jQuery?

Basically the structure:

<form id="myForm">
  <iframe>
    <!-- Normal HTML headers omitted -->
    <input type=radio name="myRadio" value=1>First
    <input type=radio name="myRadio" value=2>Second
    <input type=radio name="myRadio" value=3>Third
  </iframe>
  <input type=button value="Submit" />
</form>

I tried various examples from the net such as

$("input[@type=radio][@checked]");

But failed. Even with jQuery form plugin's .fieldValue() failed.

From stackoverflow
  • try $('#myForm iframe').contents().find('input[name=myradio]').val() I'll assume that the iframe contents have already been loaded and are accessable e.g same domain

    SyaZ : Nice, but I get "on" eventhough nothing is being selected...
    SyaZ : Actually, this worked. I used instead of value=1. My fault, typo.
  • Unless jQuery does some magic I'm not aware of, accessing another frame's DOM requires a little trickery. This may work:

    var frameDocument = $('#myForm iframe').contentDocument || $('#myForm iframe').contentWindow.document;
    $(frameDocument).find('input[type=radio][checked]');
    

    And, note this from the jQuery documentation:

    Note the "@" before the attribute name was deprecated as of version 1.2.

0 comments:

Post a Comment