1

I'm trying to count the section tags on a page but whenever this script runs it returns a 0. Any ideas?

    var sectionCount = $( "section").length;
    console.log(sectionCount);

The HTML I'm counting from is as follows:

    <body>
        <div class="container">
            <section class="slide">1</section>
            <section class="slide">2</section>
            <section class="slide">3</section>
        </div>
    </body>
Pokechu22
  • 4,790
  • 8
  • 35
  • 59
Jackanapes
  • 849
  • 1
  • 7
  • 10

1 Answers1

2

Just add a $(document).ready() clause so that it runs after the html DOM has been loaded.

$( document ).ready(function() {
  var sectionCount = $( "section").length;
  console.log(sectionCount);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
        <div class="container">
            <section class="slide">1</section>
            <section class="slide">2</section>
            <section class="slide">3</section>
        </div>
    </body>
Wold
  • 923
  • 1
  • 11
  • 25