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 divs class nav-img, have width of 20%. want divs 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

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -