-1

Is there a way to set style.top to be a variable instead of px?

I want the div to change a absolute position when i push a button to a random value instead of a constant

<div class="dot" id="dot1" style="top:180px;left:280px"></div>

function myFunction1(){
    var x1 = Math.floor((Math.random() * 150) + 75);
    var y1 = Math.floor((Math.random() * 150) + 75);

    console.log(x1);
    console.log(y1);


   //this works
   document.getElementById("dot1").style.top = "100px";
   document.getElementById("dot1").style.top = "100px";

   //this doesnt
   document.getElementById("dot1").style.top = x1;
   document.getElementById("dot1").style.top = y1;
   }
Vetlesen
  • 3
  • 4

1 Answers1

0

document.documentElement.style.top = x1. Same thing with left property.

Bwaxxlo
  • 1,860
  • 13
  • 18