title: Is there a way to uniquely identity a dynamically added element?
body: <p>I am trying to <code>dynamically</code> <code>load</code> a bunch of <code>posts</code> from a API and then implement a <code>like button</code> for each of them.</p>
  <pre><code>function load_allposts(){
      fetch(&quot;/posts&quot;)
      .then(response =&gt; response.json())
      .then(posts =&gt; {
          var enc = document.createElement('div');
          enc.className = &quot;post-enc&quot;;
          let s = ``;
          posts.forEach(element =&gt; {
              s += `&lt;div class=&quot;p-container&quot;&gt;
                      &lt;div&gt;
                          &lt;button type=&quot;button&quot; class=&quot;btn btn-link&quot; class=&quot;profile-btn&quot; data-id=${element[0].author_id}&gt; ${element[0].author_name} &lt;/button&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;post-body&quot;&gt;
                          ${element[0].body}
                      &lt;/div&gt;
                      &lt;div class=&quot;p1&quot;&gt;
                          &lt;span class=&quot;like-status&quot;&gt;${element[0].likes}&lt;/span&gt;  people like this
                          &lt;button class=&quot;like-btn&quot;&gt;${element[1]}&lt;/button&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;post-time&quot;&gt;
                          ${element[0].timestamp}
                      &lt;/div&gt;
                  &lt;/div&gt;`;
          });
          enc.innerHTML = s;
          document.querySelector('#all-posts').appendChild(enc);
      });
  }
  </code></pre>
  <p>I would to like to modify the <code>&lt;span class=&quot;like-status&quot;&gt;</code> element when I click the <code>&lt;button class=&quot;like-btn&quot;&gt;</code>. The only way that I can think of to get a reference to <code>&lt;span class=&quot;like-status&quot;&gt;</code> is by adding a <code>ID</code> to it by implementing some kind of <code>counter</code>, which I feel is more like a hack rather than real solution.</p>
  <p>I tried googling but almost all solutions involved <code>JQuery</code>, which I am not familiar with. Any help would be appreciated.</p>