Get element position in browser with JavaScript

If you use table cells as menu buttons and autosize them, this code finds the left position of element with id 'menubutton', and then places element with id 'menubody' to the same left position:

var mbtn = document.getElementById('menubutton').getBoundingClientRect();
var mbody = document.getElementById('menubody'); = mbtn.left+'px';

Or using jQuery

var newLeft = $('#menubutton').offset().left;
Enter your comment:
243 -10 = 
  • development/js/get_elem_pos.txt
  • Last modified: 2019/10/31 09:04
  • by