It is necessary to transfer diva width which value is calculated in JS.

<div id="score"></div> 

It is necessary for #score to assign the width of the variable score , which is equal to 20*2.5 , in percent

    1 answer 1

    To transfer a style value to an element, you need to refer to the element, use the style function and prescribe what exactly you want to change.

    Option 1 :

    The style is applied directly through the variable names.

    Example:
    To set the background-color , I will write obj.style.backgroundColor

     var score = 20 * 2.5; document.querySelector('#score').style.width = score + '%'; 
     #score { height: 100px; background-color: red; } 
     <div id="score"></div> 

    Option 2 :

    Styles are applied via variables from CSS

    Example:
    To set the background-color , I will directly write obj.style['background-color']

     var score = 20 * 2.5; document.querySelector('#score').style['width'] = score + '%'; 
     #score { height: 100px; background-color: red; } 
     <div id="score"></div> 

    • You rejected my edit - how do options 1 and 2 specifically differ? - br3t
    • @ br3t, 1) You did not write correctly, this is for styles with a hyphen, I can write style.backgroundColor . 2) It seems to me that the difference is obvious - Yuri
    • Please add information about the differences between these options in response. - br3t
    • @ br3t, I do not see the need. The answer is not even accepted. Well, except for future users - Yuri
    • style['background-color'] - for some reason it seems to me that this is not according to the standard ... - Qwertiy