Javascript DOM How to - Access element nested in multiple div








Question

We would like to know how to access element nested in multiple div.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!-- w  w  w  . j a va 2 s  . co  m-->
var el = document.querySelector("div div div a"),
    value = el.innerHTML;
console.log(value);
}
</script>
</head>
<body>
  <div>
    <div>
      <a>Not this one, only two divs</a>
    </div>
    <div>
      <div>
        <a>This one!</a>
      </div>
    </div>
  </div>
</body>
</html>

The code above is rendered as follows: