Javascript DOM How to - Define an alias for a document object outside the function scope








Question

We would like to know how to define an alias for a document object outside the function scope.

Answer


<!DOCTYPE html>
<html>
<body>
  <div id="pic1">The #pic1 element</div>
<script type='text/javascript'>
<!--from w w w  . j av  a  2  s.c o  m-->
    var pic1 = document.getElementById('pic1');
    var someFunc = function () {
        console.log('pic1: ' + pic1);
        pic1.style.left = "100px";
    }
    someFunc();

</script>
</body>
</html>

The code above is rendered as follows: