title: Add Class To Closest Element
body: <p>I'm trying to add a class to dynamically created objects, but there is something that is going wrong here. And I can't get it to work!</p>
  
  <p>Any ideas? </p>
  
  <pre><code>&lt;span class="status"&gt;&lt;div class="like_button"&gt;&lt;/div&gt;&lt;/span&gt;
  
   &lt;script&gt;  
     $(this).closest('span').find('.like_button').addClass('liked')
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Thing is I am having a Facebook like button (created with the API not the regular one) inside each of the elements, and needs to check its status before I can add class "liked" or "unliked". So all the div's with class "item" is created in a PHP loop. And also I understand its not good to run the script several times in a loop. But I need to get further in the testings here :)</p>
  
  <pre><code>&lt;div class="item"&gt;
   &lt;span class="status"&gt;&lt;div class="like_button"&gt;&lt;/div&gt;&lt;/span&gt;
   &lt;script&gt;  
   $(this).closest('span').find('.like_button').addClass('liked')
   &lt;/script&gt;
   &lt;/div&gt;
  
   &lt;div class="item"&gt;
   &lt;span class="status"&gt;&lt;div class="like_button"&gt;&lt;/div&gt;&lt;/span&gt;
   &lt;script&gt;  
   $(this).closest('span').find('.like_button').addClass('liked')
   &lt;/script&gt;
   &lt;/div&gt;
  
   &lt;div class="item"&gt;
  
   &lt;span class="status"&gt;&lt;div class="like_button"&gt;&lt;/div&gt;&lt;/span&gt;
    &lt;script&gt;  
   $(this).closest('span').find('.like_button').addClass('liked')
   &lt;/script&gt;
   &lt;/div&gt;
  
   &lt;div class="item"&gt;
   &lt;span class="status"&gt;&lt;div class="like_button"&gt;&lt;/div&gt;&lt;/span&gt;
    &lt;script&gt;  
   $(this).closest('span').find('.like_button').addClass('liked')
   &lt;/script&gt;
   &lt;/div&gt;
  </code></pre>