How in js to simulate a calc?

Css has a calc.
How can I use it in js?
You need to set the width.
var calc = "calc" + "(" + (c + "%") + "-" + (5 + "px") + ")";
document.element.style.width = calc;

Gives calc(100%-5px) in the console, but doesn't want to assign the width of this value.
July 2nd 19 at 18:23
4 answers
July 2nd 19 at 18:25
Solution
everything works well
document.getElementById("bla").style.width = "calc(100% - 15px)";
: https://jsfiddle.net/iBird/n0hyhecc/ everything works - Rosendo_Hess commented on July 2nd 19 at 18:28
really working, then where I have mistake in
var calc = "calc" + "(" + (c + "%") + "-" + (5 + "px") + ")" ? - Ewell commented on July 2nd 19 at 18:31
: and XS. just don't write well))) - Rosendo_Hess commented on July 2nd 19 at 18:34
: how can I then "calc(100% - 15px)" to put the variable? - Ewell commented on July 2nd 19 at 18:37
I'm above threw https://jsfiddle.net/iBird/n0hyhecc/ there both values in variables - Rosendo_Hess commented on July 2nd 19 at 18:40
: so if the direct write of a single variable, I want to ask https://jsfiddle.net/iBird/n0hyhecc/1/ - Rosendo_Hess commented on July 2nd 19 at 18:43
July 2nd 19 at 18:27
Solution
just right there in the console tested - works
document.getElementById('js-canvas').style.width = 'calc(100% - 5px)';

just do not understand why?
Blame the bike
var calc = "calc(" + (c + "%") + "-" + (5 + "px") + ")";
where in this case the error?
'calc(100% - 5px)' strictly so ask - it will work, but it is necessary, as above. - Rosendo_Hess commented on July 2nd 19 at 18:30
: need spaces around minus - Ewell commented on July 2nd 19 at 18:33
July 2nd 19 at 18:29
Solution
var calc = "calc(" + (100 + "%") + " - " + (5 + "px") + ")";
document.getElementById("classname").style.width = calc;
console.log(calc)
asked, not working - Rosendo_Hess commented on July 2nd 19 at 18:32
Works. - Ewell commented on July 2nd 19 at 18:35
You just have to add the spaces where the minus sign is ignored otherwise. - Rosendo_Hess commented on July 2nd 19 at 18:38
https://jsfiddle.net/vd631xo3/4/ - Ewell commented on July 2nd 19 at 18:41
July 2nd 19 at 18:31
And it is possible so to be perverted
var width = document.getElementById("id").parentNode.offsetWidth - 5;
document.getElementById("id").style.width = width + 'px';
value is not fixed, there is a gross example. - Rosendo_Hess commented on July 2nd 19 at 18:34

Find more questions by tags JavaScript