0

I'm tried to build the effect like this: click .labelElment to change itselvse size, and click another block or a.close to recovery .labelElment's size.

take look the html:

<div id="labelGroup">
        <div  class="labelElement c1">
            <div class="panel">
                <span class="avatar"></span>
                  <ul class="labelGroup">
                    <li class="l1">Label 1</li>
                    <li class="l2">Label 2</li>
                    <li class="l3">Label 3</li>                
                  </ul>
             </div>
             <div class="info">
                 <ul>
                     <li>Information</li>
                     <li><a href="###" class="close">close</a></li>

                 </ul>
            </div>
        </div>


        <div  class="labelElement c2">Block</div>    


    </div>

And jquery code:

$('#labelGroup').delegate( '.labelElement', 'click', function(){

 $('.labelElement').removeClass('s1')
 $(this).addClass('s1');
 $('#labelGroup').isotope('reLayout');
});

$('.close').click( function(e){
        $(this).parents(".labelElement").removeClass("s1");
 });

in the .delegat(), removeClass work well, but in the .click(), it doesn't work, so what's the problem?

2 Answers2

0

You may try this:

$(this).closest('.labelElement').removeClass("s1");
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Zainab
  • 25
  • 3
  • Guesses are not answers. Try posting as a comment in future, and make an answer if it works. – Dave Sep 21 '13 at 10:29
  • Adding comments link is not enabled for me as I am a new user . – Zainab Sep 21 '13 at 10:39
  • In that case I recommend trying your suggestion first (jsfiddle is highly recommended) so that you can be more certain. When you have enough rep, you can post quick suggestions when it is appropriate. – Dave Sep 21 '13 at 10:43
0

Your click is bubbling up the DOM hierarchy, thus also triggering the click event on .labelElement and re-adding the just removed class.

Use e.stopPropagation() to prevent bubbling.

https://api.jquery.com/event.stopPropagation

Or see answer to this question:

.parents().removeClass() not working

Community
  • 1
  • 1
Martin Krung
  • 1,098
  • 7
  • 22