2

Can I have jQuery on my page that adds a CSS class to a div thats within an iframe?

Thanks

UPDATE. For testing purposes Ive added the following. I would have thought that it would add a class of 'red' to all p tags in any iframe, but its not doing anything:

$(document).ready(function() { $('iframe') .contents() .find('p') .addClass('red'); });

Thanks

5
  • can you supply more details please? Commented Mar 24, 2011 at 9:38
  • @jackjoe: seems pretty clear to me what he's looking for? Commented Mar 24, 2011 at 9:40
  • 4
    maybe, but the OP doesn't say if it's cross domain, that changes everything. Commented Mar 24, 2011 at 9:49
  • No, its the same domain. Thanks Commented Mar 24, 2011 at 11:35
  • @jackjoe: Good point, hadn't considered that. Commented Mar 24, 2011 at 12:16

2 Answers 2

1

If it's on the same domain, you should be able to.

Try using something like this:

$('#iframe-id')
  .contents()
  .find('.my-div')
    .addClass('my-other-class');
Sign up to request clarification or add additional context in comments.

3 Comments

My iframe doesn't have an ID. Ive tried iframe,iFrame and frames but it doesn't seem to be selected. Thanks
As you have mentioned elsewhere in this post, you seem to be able to target it through another element with an Id, so try using that $('#mydivid iframe') instead of $('#iframe-id'). It should work if you're not trying to do this on an external webpage.
the code didnt work for me, ive posted my attempt in my main question at the beginning. Thanks
0

You can target a jquery selector on a specific element. In this case you will want to target the id / class of your div within the frames collection of your current page and perform the action.

See the following;

$('#mydivid', frames['myiframeid'].document).addClass('myclass');

5 Comments

My iframe doesn't have an ID. How can I select any iframe in a div? The, how can I choose which div inside it to add a class to? Thanks
@jdln: Why can't you just add an id='mydivid' to your iframe and then target it?
Do you mean add it with jQuery or actually add it to the original html? Im using a module so it would be a little tricky to find the code to do this. Im sure its not impossible, but is there another way of selecting it with jQuery? If I was trying to select a div with jQuery that didnt have an ID there would be other ways of doing it. Thanks
@jdln: Add it to the original HTML. If you can't ammend the original HTML and you are sure there is only 1 iframe on the page then you could always reference it using window.document.frames(0).document instead.
is there a way i can reference an iframe within a certain div? I can guarantee that its the only iframe in a div with an id, so can I select it that way? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.