views:

65

answers:

5

How do I fade out all the images inside the class bMenu that are not #b2 with jQuery? Thanks.

<div class="bMenu" id="b1"><img src='b1.jpg'></div>
<div class="bMenu" id="b2"><img src='b2.jpg'></div>
<div class="bMenu" id="b3"><img src='b3.jpg'></div>
+1  A: 
$('img', '.bMenu:not(#b2)').fadeOut();
Per Holmäng
the DIV means it's the CONTEXT of IMG (second parameter to jQuery function), so this answer should work also as yours gnarf :)
Juraj Blahunka
I edited my post without noting it, sorry about that. So gnarf's comment was correct..
Per Holmäng
@juraj - yeah he had `$('img:not(#b2)', '.bMenu');` when I posted that comment :)
gnarf
@grarf :-D that's why Edit changes should be sometimes appended without allowing rewrite
Juraj Blahunka
+2  A: 

Literal answer:

$(".bMenuL:not(#b2) img").fadeOut();

Assuming you want to make sure that the #b2 img is shown as well:

$("#b2 img").fadeIn();
gnarf
I'd really like to know why this got a -1
gnarf
A: 

Try this:

#('.bMenu > img').each(function(it){
    if(it.attr('id') != 'b2'){
        it.fadeOut();
    }
});

Might be a way to do it with pure selectors but this should work.

Added Later:

Ok, I went and did a test... here is what I came up with:

$('div[id!=b2].bMenu > img').each(function(){
    $(this).fadeOut();
});

This selector will return two images, not the one with b2.

cjstehno
Again, `#b2` is the `div` not the `img`
gnarf
Oops... then it.parent().attr('id') != 'b2' or something similar. I think the other answers should work too.
cjstehno
See my "added later" section for an updated version.
cjstehno
+1  A: 

Try

$('.bMenu:not(#b2) img').fadeOut('slow');
rosscj2533
+1  A: 

Get it all done at once with chaining:

$("#b2 img").show().parent().siblings(".bMenu").find("img").fadeOut();
Jonathan Sampson
Nice chain there.
gnarf