user1804234
user1804234

Reputation: 341

I can't get the width of a div in javascript

My code positions a div where my mouse is at the time and is supposed to move the div to the left the same amount as its width if it is too close to the right side of the page. But I can't seem to get the width of the div. Here is my code:

function position(e) {
  document.getElementById(tt).style.visibility = "visible";
  if (document.body && document.body.offsetWidth) {
    var winW = document.body.offsetWidth;
    var winH = document.body.offsetHeight;
  }
  if (e.pageX || e.pageY) {
    l = e.pageX;
    u = e.pageY;
  } else if (e.clientX || e.clientY) {
    l = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    u = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
  var divL = document.getElementById(tt).style.width;
  if ((winW - l) <= 320) {
    l = l - divL;
  }
  document.getElementById(tt).style.top = (u + 10) + 'px';
  document.getElementById(tt).style.left = (l + 10) + 'px';

}

Upvotes: 0

Views: 128

Answers (1)

KingKongFrog
KingKongFrog

Reputation: 14429

var width = document.getElementById(tt).offsetWidth;

Upvotes: 1

Related Questions