0

Using javascript variable for setting CSS style values not work, this is my javascript code:

imageH.src = "image.jpg";
imageH.onload = function() {
    var valueH = (this.height/this.width)*100;
  }

var div1 = document.createElement('div');
div.style.paddingTop = valueH + '%';

this not work, but if i use the value betwen "" :

div.style.paddingTop = "60%";

it work.

Thanks!!

gurrumo
  • 83
  • 2
  • 10

1 Answers1

0

Move valueH outside of function:

imageH.src = "image.jpg";

var valueH;

imageH.onload = function() {
    valueH = (this.height/this.width)*100;
  }

var div1 = document.createElement('div');
div.style.paddingTop = valueH + '%';
Axel
  • 4,365
  • 11
  • 63
  • 122