Reputation: 6862
I am a beginner with jQuery and I am trying to modify a script. The script demo is here. Right now, they add a line above the selected tab, which I don't want. What I want to do is just add a class to the anchor like a
<a class="tab active" href="#">Tab Name</a>
that way I can just use CSS to change the background color or something for the active buttons.
The code below is what they currently have for when you click on a tab.
the_tabs.click(function(e){
/* "this" points to the clicked tab hyperlink: */
var element = $(this);
/* If it is currently active, return false and exit: */
if(element.hasClass('.active')) return false;
$(this).addClass('active')
Upvotes: 1
Views: 2402
Reputation: 25776
the_tabs.click(function(e){
var element = $(this);
if( element.hasClass('active') ) {
return false;
}
else {
the_tabs.removeClass('active');
element.addClass('active');
}
}
Upvotes: 3
Reputation: 472
Try this:
the_tabs.click(function(e){
/* "this" points to the clicked tab hyperlink: */
var element = $(this);
/* If it is currently active, return false and exit: */
if(element.hasClass('active')) return false;
/* Detecting the color of the tab (it was added to the class attribute in the loop above): */
var bg = element.attr('class').replace('tab ','');
$('ul.tabContainer .active').removeClass('active');
element.addClass('active');
/* Checking whether the AJAX fetched page has been cached: */
if(!element.data('cache'))
{
/* If no cache is present, show the gif preloader and run an AJAX request: */
$('#contentHolder').html('<img src="img/ajax_preloader.gif" width="64" height="64" class="preloader" />');
$.get(element.data('page'),function(msg){
$('#contentHolder').html(msg);
/* After page was received, add it to the cache for the current hyperlink: */
element.data('cache',msg);
});
}
else $('#contentHolder').html(element.data('cache'));
e.preventDefault();
})
Also check my blog (website) for some nice jQuery guides. :)
Upvotes: 0
Reputation: 9031
instead of
if(element.find('.active').length) return false;
use
if(element.hasClass('.active')) return false;
or you have to use .filter
instead of .find
, find is trying to find a child node that has the class .active but .filter
filters out the collection to find the matching css-selecotr
Upvotes: 1
Reputation: 76870
You just need:
$(this).addClass('nameOfYourClass')
to add a class to the clicked object
Upvotes: 2