Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2013, 08:12 PM   PM User | #1
dylanbaumannn
Regular Coder

 
Join Date: Feb 2012
Location: Nebraska, USA
Posts: 132
Thanks: 8
Thanked 19 Times in 19 Posts
dylanbaumannn is an unknown quantity at this point
Question .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!

Last edited by dylanbaumannn; 01-08-2013 at 08:19 PM.. Reason: adding jsFiddle link
dylanbaumannn is offline   Reply With Quote
Old 01-08-2013, 08:26 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,579
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
dylanbaumannn (01-08-2013)
Old 01-08-2013, 08:32 PM   PM User | #3
dylanbaumannn
Regular Coder

 
Join Date: Feb 2012
Location: Nebraska, USA
Posts: 132
Thanks: 8
Thanked 19 Times in 19 Posts
dylanbaumannn is an unknown quantity at this point
Ah, frameworks are hard :p

Quote:
Originally Posted by VIPStephan View Post
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 is offline   Reply With Quote
Old 01-08-2013, 09:33 PM   PM User | #4
dylanbaumannn
Regular Coder

 
Join Date: Feb 2012
Location: Nebraska, USA
Posts: 132
Thanks: 8
Thanked 19 Times in 19 Posts
dylanbaumannn is an unknown quantity at this point
Thumbs up

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);
});
dylanbaumannn is offline   Reply With Quote
Reply

Bookmarks

Tags
addclass dynamic elements

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:21 AM.


Advertisement
Log in to turn off these ads.