Reputation: 2310
$('.selected').removeClass('selected');
$(this).addClass('selected');
Before adding the 2 lines of code above, the whole function worked. Now only those two lines work when I click and the rest of my code doesn't. If I remove these two lines, the code works again. What could removing a class and adding a class possibly do to ruin the rest of this function? What am I doing wrong here?
$(document).ready(function(){
var subject;
$('.subject').mouseenter(function(){
if ( $(this).hasClass(subject) ) {
return 0;
}
else {
$(this).find('.info, img').fadeTo('fast', 1);
}
});
$('.subject').mouseleave(function(){
if ( $(this).hasClass(subject) ) {
return 0;
}
else {
$(this).find('.info').fadeTo('fast', 0);
$(this).find('img').fadeTo('fast', 0.8);
}
});
// BELOW IS WHERE THE PROBLEM IS
$('[class$=-link]').click(function(){
$('.selected').removeClass('selected'); // Why do these lines break the
$(this).addClass('selected'); // rest of this function?
$('.' + subject).find('.info').fadeTo('fast', 0);
$('.' + subject).find('img').fadeTo('fast', 0.8);
subject = $(this).attr('class').replace("-link","");
$('.'+ subject ).find('.info, img').fadeTo('fast', 1);
});
});
Upvotes: 0
Views: 299
Reputation: 18078
As far as I can tell, you want something like this (avoiding the need to change the HTML) :
$('[class$=-link]').click(function(){
$('.selected').removeClass('selected');
$('.' + subject).find('.info').fadeTo('fast', 0).end().find('img').fadeTo('fast', 0.8);
subject = $(this).attr('class').replace("-link","");//execute this line while "selected" class is not present
$('.'+ subject).find('.info, img').fadeTo('fast', 1);
$(this).addClass('selected');//add after everything else is complete
});
Upvotes: 2