9

I want change class property on an iframe I use .contents() which is perfectly working. But I want to do this using JavaScript. My code is:

var $c = $('#myframe').contents();              
$c.find('.inner-wp').css('margin','0')
Rob W
  • 341,306
  • 83
  • 791
  • 678
Jitender
  • 7,593
  • 30
  • 104
  • 210

1 Answers1

8

.contents() on a frame returns the document within the frame. So, you're looking for:

var frame = document.getElementById('myframe');
var c = frame.contentDocument || frame.contentWindow.document;

Note: This won't work if the frame is from a different origin.
The remaining part of the code:

var inner_wp = c.getElementsByClassName('inner-wp');
for (var i=0; i<inner_wp.length; i++) {
    inner_wp[i].style.margin = '0';
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • @amit See http://stackoverflow.com/questions/1818865/do-we-have-getelementsbyclassname-in-javascript. – Rob W Jul 23 '12 at 17:35
  • Please note that `contents()` in general returns the children of each element in the set of matched elements, including text and comment nodes. – jesper Nov 07 '14 at 12:33