Your code:
function resizeImagePreview(){
var imageHolder = document.getElementById("image-holder");
imageHolder.style.width = "25%";
console.log(imageHolder.style.width);
imageHolder.style.height = imageHolder.style.width + "px";
console.log(imageHolder.style.height);
}resizeImagePreview();
In the third line, you assign the value 25%
to the attribute width
. In the fifth line, you assign the attribute height
the value of width
concatenated with the string px
. whereas the attribute will have the value 25%
, given row 3, the value of height
will be 25%px
, that doesn’t make any sense and the browser probably discards.
Now, if your intention is to get the value in pixels that the width
has after being assigned 25%
, then you must use the property offsetWidth
of the element. It would look something like:
function resizeImagePreview(){
var imageHolder = document.getElementById("image-holder");
imageHolder.style.width = "25%";
console.log(imageHolder.style.width);
imageHolder.style.height = imageHolder.offsetWidth + "px";
console.log(imageHolder.style.height);
}resizeImagePreview();
Take the example:
function resizeImagePreview(){
var imageHolder = document.getElementById("image-holder");
imageHolder.style.width = "25%";
console.log(imageHolder.style.width);
imageHolder.style.height = imageHolder.offsetWidth + "px";
console.log(imageHolder.style.height);
}resizeImagePreview();
#image-holder {
background: blue;
}
<div id="image-holder"></div>
First you assign "25%" to
width
, then defines thatheigth
will bewidth + "px"
, which results in25%px
. It doesn’t seem to make sense. What are the values displayed on the console?– Woss