1

All in the .page-wrapper should get the opacity class -- this works but the button should stay with opacity 1.

I tried this but it doesn't work (the button is inside the page-wrapper)

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $(".page-wrapper:not('button')").toggleClass("opacity")
    });
});
</script>
<style>
.opacity
{
    opacity:0.4;
}
<button>Toggle class</button>

here is a fiddle http://jsfiddle.net/SY7Np/ display:none is better for my site instead of opacity:0.0

7
  • 1
    If you hide some element containing child elements, also the childs will be hidden.. Commented Jun 4, 2014 at 23:02
  • 1
    This is standard behavior of Opacity. It propagates down and apply the effect to everything inside. Use rgba() instead. Commented Jun 4, 2014 at 23:03
  • @NawedKhan means rgba() color for backround.. for example: background: rgba(0,0,0,0.5) will give you black background with 50% opacity. Commented Jun 4, 2014 at 23:03
  • ok thx $(".page-wrapper").toggleClass("opacity") this works Commented Jun 4, 2014 at 23:04
  • but I want all with opacity not only the background-color how can I edit the button to opacity 1 Commented Jun 4, 2014 at 23:12

2 Answers 2

1

You almost had it you are either missing a space or an ' > '

$(document).ready(function () {
  $("button").click(function () {
     $(".page-wrapper > :not(button)").toggleClass("opacity")
 });
});

additionally you could do, depending on nesting:

     $(".page-wrapper :not(button)").toggleClass("opacity")

see fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

thx :-) I'm just curious what does the ">" before the :not... ?
The > means only first children, without it will select all children regardless of depth (children, grand-children, grand-grand-children...).
0

One approach would be:

.opacity,
.opacity :not(button) {
    color: rgba(0,0,0,0.2);
    background-color: rgba(255,255,255,0.2);
}

JS Fiddle demo.

References:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.