javascript - Set height to all divs with a specific class using pure js -
hi i'm trying don't think should hard in javascript can't work. have number of div
s class nav-img
, have width of 20%. want div
s have same aspect ratio, no matter size of window. height should width/1.35. here tried :
var vnavimg = document.getelementsbyclassname("nav-img"); var vnavwidth = window.getcomputedstyle(vnavimg).width; window.onload = setheight; function setheight() { vnavimg.style.height = vnavwidth/1.35+"px"; }
so close, method getelementsbyclassname()
return array of objects have given class argument, should loop through them , assign new height :
var vnavimg = document.getelementsbyclassname("nav-img"); for(var i=0;i<vnavimg.length;i++){ vnavimg[i].style.height = vnavwidth/1.35+"px"; }
or using function setheight()
:
var vnavimg = document.getelementsbyclassname("nav-img"); for(var i=0;i<vnavimg.length;i++){ setheight(vnavimg[i]); } function setheight(div) { div.style.height = vnavwidth/1.35+"px"; }
hope helps.
Comments
Post a Comment