Monthly Archives: October 2012

Add CSS Class Recursively With jQuery

The Function:

function addCSSClassRecursively(topElement, CssClass) {
    $(topElement).addClass(CssClass);
    $(topElement).children().each(
            function() {
                 $(this).addClass(CssClass);
                 addCSSClassRecursively($(this), CssClass);
            });
}

Pretty simple. The first line adds the class to the element passed in. Next, using the jQuery.children() and jQuery.each() functions it iterates through each child element adding the CSS Class and then calls itself to add the CSS class to each of the child’s children elements.

Calling it:

$(function() {
   addCSSClassRecursively($('#div1'), 'MasterCssClass');
});

In my implementation I call the function when the DOM is fully loaded, using the jQuery.ready() function. I just pass in the parent or top level element I want to start adding the class at and the functions does the rest.

Advertisements

Turn On The Music And Run

Over my running years I’ve gone back and forth between no music while I run and having music.  Usually, when I run inside on the treadmill I listen to music to pass the time and keep it interesting.   However, when I run outside, most often, I leave music at home.   Mostly, I do this because I find music a distraction.  Technical issues and mechanical failures aside, I find it also distracts me from my run.  I find that without music I “stay in my run” and get more out of it.  I’m able to concentrate on my form and pace better.

Having said that, sometimes music might be just what you need. I’ve been struggling with running a lot lately.  I’ve struggled to find time. I’ve struggled to set goals.  And I’ve struggled to improve.  Today,  I decided to change it up and take some music with me on my run.  And to my surprise it really helped.  It took my mind of some aches and pains when I got started and to my surprise didn’t seem to distract me as much as it has in the past.  Perhaps I’ve grown as a runner more than  I thought?

The moral of the story is, you’re a runner so get out there and run!  And, if you need to, turn on the music.


%d bloggers like this: