CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   .AddClass after element is dynamically written by script (http://www.codingforums.com/showthread.php?t=285484)

dylanbaumannn 01-08-2013 08:12 PM

.AddClass after element is dynamically written by script
 
I'm having some difficulty calling an .addClass() on a dynamically created list of items which is populated after the DOM loads.

The initial HTML starts off as:
Code:

<div id="productSlider">
  <ul class="slides">
    <li data-thumb="img1.jpg"><img src="img1.jpg" width="700" /></li>
  </ul>
</div>

but after the jquery which makes it an ImgSlider hits it adds an <ol> below ul.slides with list items within it for each of the list-items generated within the ul.slides.
Since the OL is added after the DOM has finished downloading I can't do a standard .addClass() on the OL's list-items since they don't exist yet.

The HTML turns into this:
Code:

<div id="productSlider">
  <ul class="slides">
    <li data-thumb="img1.jpg"><img src="img1.jpg" width="700" /></li>
  </ul>
  <ol class="thumbs">
    <li><img src="img1.jpg" width="107" /></li>
  </ol>
</div>

I created a small interval which checks every 10th of a second until it detects that the OL has a length greater than 0, then adds a class to the OL.
But I can't quite figure out why it's not working.

Here's the jQuery snippet I made:
Code:

jQuery(function() {
    var testing_Interval = setInterval(function(){
        if(jQuery('ol.thumbs').length > 0){
            clearInterval(testing_Interval);
            jQuery('ol.thumbs').addClass('hasItems');
        }
    },100);
 });

I also created a quick jsFiddle with an example here: jsFiddle

Any help would be AMAZING because this is really frustrating. Thanks guys! :thumbsup:

VIPStephan 01-08-2013 08:26 PM

Please tell us about your actual intention because there is definitely some better way to do what you want. Why do you need to add a class “hasItems”? Also, you need to check for the length of the list items (children) of the ol, not the ol itself.

And your fiddle seems to work, by the way, you just need to choose the right framework.

dylanbaumannn 01-08-2013 08:32 PM

Ah, frameworks are hard :p
 
Quote:

Originally Posted by VIPStephan (Post 1304913)
Please tell us about your actual intention because there is definitely some better way to do what you want. Why do you need to add a class “hasItems”? Also, you need to check for the length of the list items (children) of the ol, not the ol itself.

And your fiddle seems to work, by the way, you just need to choose the right framework.

My end intention is to add do an .addclass('NoMarginRight') to every 4th child within the ol.thumbs

dylanbaumannn 01-08-2013 09:33 PM

RESOLVED
big thanks to VIPStephan for pointing out I have to call the .length() on the li, not the ol. Got it working now! Thanks man.

Code:

jQuery(function() {
  var testing_Interval = setInterval(function(){
    if(jQuery('ol.thumbs li').length > 1){
        clearInterval(testing_Interval);
        jQuery('ol.thumbs').find(":nth-child(4n)").addClass('noMarginRight');
    }
  },100);
});



All times are GMT +1. The time now is 07:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.