Element offsetLeft Property - Javascript DOM

Javascript examples for DOM:Element offsetLeft

Description

The offsetLeft property returns the left position in pixels relative to the left side the offsetParent element.

Return Value

A Number, representing the left position of the element, in pixels

The following code shows how to get the offsetLeft position of a <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#test {// www . j  a v  a2  s. c om
  left: 100px;
  top: 50px;
  margin: 10px;
  padding: 10px;
  width: 300px;
  position: relative;
  border: 5px solid black
}
</style>
</head>
<body>

<div id="test">
  <p>This is a test. This is a test. This is a test.</p>
  <p><button onclick="myFunction()">Test</button></p>
  <p id="demo">offsetLeft: <br>offsetTop: </p>
</div>

<script>
function myFunction() {
    var testDiv = document.getElementById("test");
    var demoDiv = document.getElementById("demo");
    demoDiv.innerHTML = "offsetLeft: " + testDiv.offsetLeft + "<br>offsetTop: " + testDiv.offsetTop;
}
</script>

</body>
</html>

Related Tutorials