views:

30

answers:

3

I have a form inside an iframe which is inside a jQuery UI dialog box. The form contains a file input type. The jQuery UI dialog contains an Upload button. When this button is clicked, I would like to programmatically call the submit method. My question is how I could select the form which is in a iframe using jQuery. The following code should clarify the picture:

<div id="upload_file_picker_dlg" title="Upload file">        
    <iframe id="upload_file_iframe" src="/frame_src_url" frameborder=0 width=100% scrolling=no></iframe>
</div>

frame_src_url contains:

<form action="/UploadTaxTable" enctype="multipart/form-data" method="post" id="upload-form">            
<p>Select a file to be uploaded:</p>
<p>
    <input type="file" name="datafile" size="60">
</p>

The jQueryUI dialog box javascript code:

$('#upload_file_picker_dlg').dialog({
    ...
    buttons: {
        'Close': function() { 
            $(this).dialog('close'); 
        },
        'Upload': function() {              
            $('#upload-form').submit(); //question is related to this line
            $(this).dialog('close'); 
        }
    },
    ....
});

From the javascript code snippet above, how can I select the form with id upload-form that is in the iframe whose id is upload_file_iframe ?

A: 

Off the top of my head I'd say you might have to add the logic to the file where the iframe source is from.

zynx
Maybe that would work, but I shouldn't have to. I just need to know a jQuery selector that selects a form (or id in general) that's inside an iframe.
Khnle
+3  A: 

Accessing an element inside an iframe is tricky. You should use the following syntax:

$('#iframeID').contents().find('#upload-form').submit();

where 'iframeID' is obviously an ID you've given to the iframe.

Hope it is correct!

Davide
Yep, that's correct. I posted my answer at the same you did. But your answer is 100% correct. I selected your answer and vote up on your answer. Thanks.
Khnle
+2  A: 

The answer is:

$('#upload_file_iframe').contents().find('#upload-form').submit();

which I learned from http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/

Khnle