Javascript Event How to - Get DOM text node from point








Question

We would like to know how to get DOM text node from point.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(function(){<!--   w w w.  ja v a  2 s.  c o m-->
    $(document).on('click', function(evt) {
        var elem = document.elementFromPoint(evt.clientX, evt.clientY);
        console.log(elem);
    });
});
</script>
</head>
<body>
<html>
<body>
  some text here
  <p id="para1">lalala</p>
  bla bla
</body>
</html>
</body>
</html>

The code above is rendered as follows: