0

This is what I'm trying to do :

  • function : Create a div with random top and left value,
  • function : get the css value of this object (I'm there) and move this div :

I can't get the css value. it's return :

TypeError: undefined is not an object (evaluating 'elem1.style.top')

there is the javascript file :

var r = 50;
var rc = r /2;
var q = 0.2;
var wq = window.innerWidth/q;

function getRandomIntInclusive(min, max) 
{
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min +1)) + min;
}
function getPosition()
{
    var elem1 = document.getElementsByClassName('circle');
    var value = elem1.style.top;
    alert(value);
}
function createObject(wq,r,rc)
{
    var square = document.createElement('div');
    square.className = 'square';
    square.style.width = "" + r + "px";
    square.style.height = "" + r + "px";

    var circle = document.createElement('div');
    circle.className = 'circle';
    circle.style.width = "" + rc + "px";
    circle.style.height = "" + rc + "px";
    circle.style.top = "" + getRandomIntInclusive(0, rc) + "px";
    circle.style.left = "" + getRandomIntInclusive(0,rc) + "px";
    square.appendChild(circle);

    document.body.appendChild(square);
    getPosition()
}
createObject(wq,r,rc);

Answer : - send the div I've created to the getPosition(circle) function - write the whole animation process etc.. and apply new random values.

CharlieWhite
  • 133
  • 1
  • 12
  • 1
    Why don't you store your random generated top and left in a variable before applying it to your div? Because that basically is the position you are trying to retrieve – Thomas van Broekhoven Dec 18 '18 at 13:49
  • `document.addEventListener("load", createObject(wq,r,rc));` is pointless. You have to pass a function to addEventListener and createObject returns `undefined` – Quentin Dec 18 '18 at 13:50
  • In addition to the other comments, note that `getElementsByClassName('circle')` will return a collection, not a single object. – cody Dec 18 '18 at 13:52

0 Answers0