0

I am working on 'OnClick' functionality in jQuery. When we click Div property other divs should fade out.

Please find the code below. HTML:

<div>1</div>
<div class="ree">2</div>
<div class="foo">
  <div class = "bar">
     <div class="nike">3</div>
  </div>
</div>
<div>4</div>

jQuery:

$('.nike').on('click',function ()
              {
$('div:not(.nike)').css('opacity','0.2')

              });

Here When I click on Div Class 'Nike', Except 'Nike' all Div classes should fade out. But here All Divs including Nike is fading out.

http://jsfiddle.net/6V8hr/5/

Thanks all

7
  • 5
    Because your "nike" div is contained by other DIVs? :) Commented Oct 24, 2013 at 21:07
  • so.. should i remove those parent divs? Commented Oct 24, 2013 at 21:07
  • Check this: jsfiddle.net/6V8hr/8 Commented Oct 24, 2013 at 21:09
  • I will be needing that for future implementations.. :( is there anyway to do without removing those? Commented Oct 24, 2013 at 21:09
  • You could do it the brute force way and explicitly add the opacity of the other divs on click Commented Oct 24, 2013 at 21:12

4 Answers 4

2

Since you have nested DIVs, those parent DIVs are getting faded out, also causing your nike div to fade out.

While this code isn't perfect... it works for what you need it to.

DEMO

$('.nike').on('click',function () {
   $('div').not('.foo').not('.bar').not('.nike').css('opacity','0.2')
});

So I'm basically listed the classes in your tree containing nike, making sure none of those are affected.

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

2 Comments

Perhaps having a class for the divs that might be faded (including the one you don't want to fade for this click) and then using .not() against that list.
That's a real lack of genericity.
1
$('.nike').on('click', function() {
    $('div:not(.nike):not(:has(.nike))').css('opacity', '0.2');
});

Comments

1

Well this code seems better suited

$('.nike').on('click', function () {
  $('div').each(function () {
    if ($('.nike').closest($(this)).length == 0) {
        $(this).css('opacity', '0.2');
    }
  })
});

http://jsfiddle.net/H17737/nr5bL/

1 Comment

@H17737.. thank you so much.. Is there anyway to bring those numbers back to original if i click back on div class 'Nike' again?
1

I read the HTML all 10 kinds of wrong. Below is the revised, short sweet and to the point. Add class="hideThis" to any divs you would want to be "hidden". If you have multiple sibling divs that you want to hide/show on click, you could give them all the hideThis class and replace the $('.nike') with $('.hideThis')

$('.nike').click(function() {
  $(this).css('opacity','1'); //in case something else got clicked & hid this
  $('.hideThis').not($(this)).css('opacity','0.2'); //hide everything else
}

2 Comments

Is this right? <div>1<div> is not a parent div of nike. foo and bar are parent divs... but foo would be a sibling of <div>1<div>.
@Charlie74: thats correct.. but i tried that method in jsfiddle.. didn't work.. :(

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.