title: JavaScript foreach failed
body: <p>I have a JS foreach function like below:</p>
  <p><strong>Edit: my minimal reproducible example</strong>
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var liArray = document.getElementsByClassName("listItem");
  liArray.forEach(function(item) {
    if (item.getAttribute("data-code").substring(0, 2) == 15) {
      console.log("success");
    }
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;ul&gt;
    &lt;li class="item&gt;
      &lt;div data-code="151" class="listItem"&gt;example&lt;/div&gt;
    &lt;/li&gt;
    &lt;li class="item&gt;
      &lt;div data-code="152" class="listItem"&gt;example&lt;/div&gt;
    &lt;/li&gt;
    &lt;li class="item&gt;
      &lt;div data-code="153" class="listItem"&gt;example&lt;/div&gt;
    &lt;/li&gt;
    &lt;li class="item&gt;
      &lt;div data-code="154" class="listItem"&gt;example&lt;/div&gt;
    &lt;/li&gt;
    &lt;li class="item&gt;
      &lt;div data-code="155" class="listItem"&gt;example&lt;/div&gt;
    &lt;/li&gt;
    &lt;li class="item&gt;
      &lt;div data-code="156" class="listItem"&gt;example&lt;/div&gt;
    &lt;/li&gt;
  &lt;/ul&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Than I got <strong>150</strong> times of <code>success</code> in my console. But when I changed <code>console.log</code> to <code>item.className = &quot;disabled&quot;</code>. Like:</p>
  <pre><code>liArray.forEach(function(item) {
    if (item.getAttribute(&quot;data-code&quot;).substring(0, 2) == 15) {
      item.className = 'disabled';
    }
  });
  </code></pre>
  <p>It turns out that only half of (<strong>75</strong>) the items have class <code>disabled</code>.</p>
  <p>I have no idea what's going wrong with my code.<br>
  Hope someone can help, thank you!</p>