Assign variable value to css style in javascript


This using javascript to modify some goes some values in the styles, for it I do some calculations, and then I assign that value to the style of the identifier that I want to modify, but it does not show me any type of change .

document.getElementById('barra_logro_mensajes').style.right = mover;

If I do it manually, there's no problem:

document.getElementById('barra_logro_mensajes').style.right = "97.6";

I've tried single quotes, double quotes, with \" , but nothing.

asked by JetLagFox 02.04.2017 в 00:28

1 answer


The error is in Assigning a value to a property css , you are sending it a number (int o float) but without a unit of measure (px , em) that is why it is not recognized since a cadena is expected. To solve, it would suffice to concatenate the value obtained by your calculations + la unidad de medida

var mover = {calculos...}
document.getElementById('barra_logro_mensajes').style.right = mover+ "px";
answered by 02.04.2017 / 01:02