1

The problem is when I click i element with ico class, I need to add class b to div, which has class a. This is what I tried.

My html code is:

 <div class="container">
    <div class="block">
        <div class="a"></div>
        <div>
            <i class="ico"></i>
        </div>
    </div>
    <div class="block">
        <div class="a"></div>
        <div>
            <i class="ico"></i>
        </div>
    </div>
    <div class="block">
        <div class="a"></div>
        <div>
            <i class="ico"></i>
        </div>
    </div>
</div>

JQuery:

jQuery(function($) {
  $('.ico').click(function() {
    $('.a').toggleClass('b');
  })

});

3 Answers 3

2

maybe you want this.

$('.ico').click(function(){
    $(this).closest('.block').find('.a').toggleClass('b');
});
Sign up to request clarification or add additional context in comments.

Comments

0

I think the event is not properly binded with ico

Try Below

$(document).ready(function () {
    $('.ico').on('click',function(){
        $('.a').toggleClass('b');
    });

});
.a {
    color:red;
}
.b {
   color : green; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div class="container">
    <div class="block">
        <div class="a">TEestete</div>
        <div>
            <i class="ico">Click</i>
        </div>
    </div>
    <div class="block">
        <div class="a">asdasdas</div>
        <div>
            <i class="ico">Click</i>
        </div>
    </div>
    <div class="block">
        <div class="a">sadasd</div>
        <div>
            <i class="ico">Click</i>
        </div>
    </div>
</div>
Fiddle Here http://jsfiddle.net/vLLtqxw2/1/

Comments

0

your question is not clear, but this is what I understood.

fiddle: https://jsfiddle.net/jn7z7k2k/2/

jQuery(function($) {
  $('.ico').click(function() {
   	$(this).parent().prev().toggleClass('b');
  });
});
.ico{
    background:red;
    display:block;
    margin:10px;
}
.block{
    background:green;
    margin:10px;
    padding:5px;
}
.a.b{
    background:blue;
}

.a {
    display:block;
    background:pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
    <div class="block">
        <div class="a">anchor</div>
        <div>
            <i class="ico">ico</i>
        </div>
    </div>
    <div class="block">
        <div class="a">anchor</div>
        <div>
            <i class="ico">ico</i>
        </div>
    </div>
    <div class="block">
        <div class="a">anchor</div>
        <div>
            <i class="ico">ico</i>
        </div>
    </div>
</div>

1 Comment

then to what do you want to add ? be a bit specific

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.