Javascript DOM How to - Get the number of elements after querySelectorAll








Question

We would like to know how to get the number of elements after querySelectorAll.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!-- w  w w . j  a  v  a2s. c  o m-->
    document.writeln( document.querySelectorAll(".player").length )
}
</script>
</head>
<body>
  <span class="player">lorem</span>
  <span class="player">ipsum</span>
</body>
</html>

The code above is rendered as follows: