Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prependTo closest specified Parent div from an iframe

I'm trying to use JQuery from inside an iframe to .prependTo a .class div inside the Parent.

This has multiples of the same .class. **EDIT And iframes

Everything is on the same domain.

So, from inside the Main document:

<div class="NewPHOTOS">
    **<!--PUT ME HERE!-->**
    <div class="LinePhoto">
        <a href="images/518279f07efd5.gif" target="_blank">
            <img src="images/thumb_518279f07efd5.gif" width="50" height="50">
        </a>
    </div>

   <iframe class="uploadLineID_55" width="800px" height="25px" src="../../uploads/uploadiframe.php" scrolling="no" seamless></iframe>
</div>

Script inside the iframe:

$(document).on("click", '#TEST', function() {

  appendImagetoParent();

});


function appendImagetoParent()  {

var data = '<div class="LinePhoto"><a href="images/TEST.gif" target="_blank"><img src="images/thumb_TEST.gif" width="50" height="50"></a></div>';

$(".NewPHOTOS", window.parent.document).each(function() { $(data).prependTo($(".NewPHOTOS"));
});

/* $(data).prependTo( $(".NewPHOTOS", window.parent.document) ); This prepends to Every .NewPHOTOS */}

I've been running around in circles and googleing for hours now. I can't figure this out.

What am I doing wrong here?

EDIT I used, works great!

 $(parent.document).find('.' + frameElement.className )
                      .closest('.NewPHOTOS')
                      .prepend( data );
like image 918
Monty Avatar asked Oct 22 '22 10:10

Monty


1 Answers

$(document).on("click", '#TEST', appendImagetoParent);

function appendImagetoParent()  {

    var div = $('<div />', {'class':'linePhoto'}),
        a   = $('<a />',   {href:'images/TEST.gif', target:'_blank'}),
        img = $('<img />', {src:'images/thumb_TEST.gif', width:'50', height:'50'});

    $(parent.document).find('.' + frameElement.className )
                      .closest('.NewPHOTOS')
                      .prepend( div.append( a.append(img) ) );
}

Get the class of the containing iFrame with frameElement.className, and use that class to find the right iFrame in the parent document, then find the closest .NewPHOTOS element, and prepend the content, created in a more jQuery'ish way.

like image 61
adeneo Avatar answered Oct 24 '22 02:10

adeneo