How to add pixels to an existing value of pixels in JavaScript?

I need to add 25px to an iframe with somewhat dynamic height, to compensate for 25px of padding that this iframe already got (and by doing so ridding end-users from the need to scroll-down these extra 25px).

I tried this:

iframeToWorkOn.height += "25px";

It added about 47525px instead just 25px and I have no clue why.

You don’t need to set it by pixels - that should be the default dimension (I think)

iframeToWorkOn.height += 25;

I still get about 47525px instead just 25, this way… :neutral_face:

I think it is probably concatenating the 25 onto the current frame height. You need to ensure it is performing an arithmetic addition.

What is the value of iframeToWorkOn.height?

From executing iframeToWorkOn.height in browser console its value is 300 (px).

Ahhh…should have looked closer and caught this - height returns a string.

So you’ll need to do something like this.

iframeToWorkOn.height = (parseInt(iframeToWorkOn.height) + 25);
1 Like

Thanks, I think it works.

Just one question why is the parseInt() method and the + 25 arithmetic operation where both wrapped in parenthesis?

You don’t need the wrapping parenthesis. I did it for readability but this works as well.

iframeToWorkOn.height = parseInt(iframeToWorkOn.height) + 25;
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.