tags:

views:

2341

answers:

5

I have 3 images and have a function so that on mouse rollover they fade in and out. I don't want to do this if the image being rolled over has the class 'selected'.

So far I have this code:

$(".thumbs").hover(function(){

if (!($(this).hasClass(".selected")){

$(this).stop().fadeTo("normal", 1.0);
},function(){
$(this).stop().fadeTo("slow", 0.3);

}

});

to me the IF statement looks like it should be:

if (!($(this).hasClass(".selected"))){

but neither work so who knows. When I implement this code my entire javascript stops working, any ideas why?

There is virtually NO information on IF statements for jQuery on Google, it's ridiculous. I would greatly appreciate any help!

+4  A: 

The problem is that your element has the class "selected" not ".selected". Remove the dot.

Georg
jQuery uses CSS-like notation for its selectors… the dot indicates that it is a class.
Ben Blank
@Ben: $(".selected") is correct but the OP wants hasClass("selected"). Note the first is a selctor the second is not.
cletus
doesn't work already tried
zuk1
this is what happens when people learn jQuery instead of JS :(
annakata
@annakata: yeah cos cross-browser with pure JS is so much fun...
cletus
+1  A: 

Maybe it should be

if (!($(this).hasClass("selected"))){

no . because it knows you're looking for a classname.

John Boker
+1  A: 

The jQuery docs on selectors tells you everything you neeed to know.

$('.thumbs:not(.selected)').hover(function(){
  // ...
})
August Lilleaas
I think this would not work if the selected item changed (assuming you called this in $(document).ready)
CodeMonkey1
I agree that this wouldn't work if called in document ready if the selected class were applied after page load.
tvanfosson
+1  A: 

Use the not function:

if ($(this).not(".selected")) {
    ...
}
Ben Blank
+1  A: 

I think your javascript isn't properly formatted -- braces in the wrong places -- and you shouldn't use the dot in the hasClass function. A little better formatting would help readability, too.

$(document).ready( function() {
    $('.thumbs').hover( function() {
                            if (!$(this).hasClass('selected')) {
                                $(this).stop().fadeTo("normal", 1.0);
                            }
                        },
                        function() {
                            $(this).stop().fadeTo("slow", 0.3);
                        }
                 );
});
tvanfosson