Search code examples
javascriptcssgetboundingclientrect

Assign a value from getBoundingClientRect


I was trying this code from W3Schools and modified it to assign the resulted LEFT value to another element (DIV)

getBoundingClientRect example

Nothing happens even if the left value 8 is correctly recovered ! Any idea on how i am trying this assigning wrong ! Thanks


Solution

  • To directly assign CSS you need to pass a valid value more on MDN to it. So it needs to have e.g. "px" or "%" at it.

    In short, it would work like that:

    document.getElementById("aaa").style.left = x + "px";