Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the JavaScript equivalent to jQuery's .contents()?

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')
like image 445
Jitender Avatar asked Jul 23 '12 16:07

Jitender


1 Answers

.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';
}
like image 195
Rob W Avatar answered Sep 30 '22 20:09

Rob W