advanced web statistics

Generic jQuery Function to Remove CSS Classes

6/10/2009 3:10:42 PM

I've been using a lot of jQuery lately in a new project and am falling in love with it!  It is a wonder why I have never used it before but am glad I was kind of forced to learn it ;)

As with anything new there is a bit of a learning curve.  The application I am working on boasts a large number of tabs for different sections of the site.  One of the requirements is to toggle the "active" tab via CSS class.  Easy right?  This was easy to do in vanilla JavaScript so it should be super-easy to do with jQuery.  It is!

$('#FooItem1').removeClass('active');
$(
'#FooItem2').removeClass('active');
$(
'#FooItem3').removeClass('active');
$(
'#FooItem4').removeClass('active');
$(
'#FooItem5').removeClass('active');

That works great but there is one caveat: adding new tabs.  If the requirements of the UI changed and we were to have to add a new tab (or 6 more) then we would have to not only change the view (or in this case partial view ;) ) but also the JavaScript functions dealing with these tabs.  Since all of our tabs are UL with stylized LI's containing anchor tags I decided to create something like this:

function RemoveActiveClassFromListItemControlByID(controlID) {
   $(
"#" + controlID).children().each(function() {
   $(
this).children("a").removeClass("active");});
}

Now this isn't that generic since I have the "a" and "active" strings hard-coded.  In this case it works for us since all the tabs are the same format and all we really need is the id of the control.  This little function will enumerate the children of the control (in this case all LI's and then enumerate the A children and remove the active css class.  Simple.  To extend this to be even more generic you could do the following:

function RemoveClassNamesFromChildElementByControlID(controlID, childElement, className) {
$(
"#" + controlID).children().each(function() {
   $(
this).children(childElement).removeClass(className);});
}

Here's an example of how you could use the function above.  Let's say that you want to enumerate all children in a OL with an id of "fooList". Each children has a span tag with a css class of BAR and you want to reset all of them when a user clicks on a hyperlink.  This would be extremely simple:

<a href="javascript:(RemoveClassNamesFromChildElementByControlID('fooList', 'span', 'BAR'));">CLICK HERE</a>

Easy.  I'm really starting to love this jQuery business.

jQuery + MVC = love

JavaScript

kick it on DotNetKicks.com

Comments


Why dont you just do
$("#" + controlID)
.find(`a`)
.removeClass(`active`)
There is no need for the each etc as find will find all child anchor tags. Also inline javascript attributes are bad, especially if your trying to be unobtrussive which is what jQuery is all about. So hookup the click event inside a document ready block.

Posted by: redsquare | 6/10/2009 6:53:14 PM

Perfect! This goes back to that whole learning curve I mentioned ;)

Code has been refactored!

Posted by: Will Asrari | 6/10/2009 6:56:18 PM

intresting coments, tanks for your contribution

Posted by: Meeting Women | 9/16/2009 10:37:22 AM

Nice blog i really like it keep it going

Posted by: Generic Viagra | 9/17/2009 11:39:24 AM

I really appreciate this info, please keep me inform

Posted by: Viagra | 9/17/2009 12:56:05 PM

wow.. keep working like this thanks!!

Posted by: Blue Eyes | 9/18/2009 4:50:18 AM

Great post thanks for sharing a informative

Posted by: Infection | 9/22/2009 3:47:46 PM

very nice post thanks!! i really like it
great post thanks for sharing a informative

Posted by: Casino Poker | 9/23/2009 6:58:13 PM

hello i realy goint to recommend this page very good

Posted by: Online Pharmacy | 10/29/2009 4:02:47 PM

jQueryI it`s completly amazinf fellas belibe me and really like this blog, has a great variety of wonderful views and advice to the user, I feel really great.

Posted by: Generic Medications | 10/29/2009 5:02:32 PM

Hi There! Really cool site . OK so I`m always searching for this kind of stuff.
I have this fascination thing. Keep up the good work

Posted by: Buy Generic Viagra | 11/24/2009 4:26:29 PM

Thanks for the information. I learned something new today!

Posted by: online slot games | 12/8/2009 7:54:07 AM

This is some very valueable information, thank you very much.

Posted by: us drugstore | 1/1/2010 3:48:43 AM

when one know IDs like ... DIv1, div2... div 10... This may be more helpful...

$("div[id*=FooItem").removeClass(`active`);

.. Anyways .. Thnkx.. I really need to know how to create generic fun with jquery...

Posted by: Rimmi Dhankhar | 1/14/2010 10:27:07 AM

Thanks for the review. I have this fascination thing. Keep up the good work

Posted by: safe ed | 1/29/2010 9:43:54 AM

Leave a Comment

   

  Enter the text to proceed!