jQuery - Change child iframe not whole page?

I'm trying to get the child iframe and set it's source in my div called "myLittleModal"

function createModalElement( modalClassName ) {
return $('body').append("<div class=\""+ modalClassName + "\"><iframe></iframe></div>");
}


var modalElement = createModalElement( "myLittleModal" );
//alert(modalElement.text());           
modalElement.find('iframe').attr('src', "modal.html");

But it's actually changing the source of not only the iframe, but another iframe as well, which isn't a child of this perticular element at all. ??? WHY!?

I thought modalElement would be a handle to the particular element i created, by which i could manipulate it's specific child iframe (not needing a class as it's a child of the class i can style).

Upon looking in the source code console in chrome, i see that it's also changing the source of another iframe on the page, which is for testing purposes now, loading a live page. WHY???

This also happens if i use the children("iframe") function as well.

Please help

Answers


Try replacing your createModalElement function with the following:

function createModalElement( modalClassName ) {
    $('body').append("<div class=\""+ modalClassName + "\"></div>");
    return $('.' + modalClassName).append("<iframe></iframe>");
}

What I did was splitting the append actions and only return-ed the 2nd one.

Your original iframe (the one that was there before the js call) most probably got wrapped around with a div called by this new function, because you used append to the body. In my modified code, I'm appending the iframe to the div that's got created, so when you apply an attribute, it will be applied to that one only. Again, I'm not 100% sure this is the answer, but for sure my code works.

Here is a demo you can fiddle with.


Need Your Help

PFQueryTableView Custom cell Separator not showing

ios swift parse-platform pfquerytableviewcontrolle pftableviewcell

In my PFQueryTableViewController, the custom PFTableViewCell's dont have any separators and i don't know why.

Updating iPhone application from 3.0 to iOS4

iphone ios4 upgrade

I was just wondering if anyone knows of any articles relating to upgrading an iPhone application from 3.0 to iOS4. (Thanks for this iWasRobbed).