An alternative to doing this would be to set the width of the banners as follows:
.banner-holder{
width:100%;
}
.carousel figure{
width:100%;
}
Or instead of setting css
, get the window width window.innerWidth
for(var i = 0; i < bannersWidth.length; i++){
bannersWidth[i].style.width == window.innerWidth + "px";
}
* Note that the Window object does not have the offsetWidth
property, possibly that is why it does not work .
* Also will not work screen.width
because it returns the width of the screen and according to the question you want the width of the browser.
Here is a working example:
function AjustaLarguras() {
var bannersWidth = document.querySelectorAll(".banner-holder,.carousel,.figure");
for (var i = 0; i < bannersWidth.length; i++) {
bannersWidth[i].style.width = window.offsetWidth + "px";
}
}
.banner-holder {
height: 50px;
border: 1px solid red;
}
.carousel {
height: 50px;
border: 1px solid green;
}
.figure {
height: 50px;
border: 1px solid blue;
}
<body onload="AjustaLarguras()">
<div class="banner-holder">
</div>
<div class="carousel">
</div>
<div class="figure">
</div>
</body>