title: How do I set data attribute value by the index of element in jQuery?
body: <p>I have multiple sibling elements, some that are added dynamically and others manually or using JS. Each one has a data attribute of "data-sec_number" with a number value that increases by one for each new element down the page. At the moment I am manually writing these in.
  E.g.</p>
  
  <pre><code>&lt;div class="container"&gt;
  &lt;div class="row resource_sec" data-sec_number=1&gt;
  &lt;div class="content sec_number" data-sec_number=1&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row resource_sec" data-sec_number=2&gt;
  &lt;div class="content sec_number" data-sec_number=2&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="row resource_sec" data-sec_number=3&gt;
  &lt;div class="content sec_number" data-sec_number=3&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>How can I get the value of attribute "data-sec_number" to match the index of the element .resource_sec within the .container element? E.g. the 3rd .resource_sec element will always have a "data-sec_number" value of 3.
  I also have the child element of .resource_sec called .sec_number, which has the same data attribute that should mirror the parent.</p>
  
  <p>The JS I have been trying to use is:</p>
  
  <p><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 items = $('.container .resource_sec');
  var lastItem = $('.container .resource_sec:last');
  var index = items.index(lastItem);
  
  $('.resource_sec').attr('data-sec_number', index);
  $('.sec_number').attr('data-sec_number', index);</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div class="container"&gt;
    &lt;div class="row resource_sec" data-sec_number=1&gt;
      &lt;div class="content sec_number" data-sec_number=1&gt;&lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="row resource_sec" data-sec_number=2&gt;
      &lt;div class="content sec_number" data-sec_number=2&gt;&lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="row resource_sec" data-sec_number=3&gt;
      &lt;div class="content sec_number" data-sec_number=3&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>