how to add an image only in checked checkbox [duplicate]

1

I would like to know how I can do to add an image with append only within span.abcd that is next to the checked checkbox.

HTML

<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
    <div class="hovereffect">
        <span class="abcd"></span>
        <img id="he" class="img-responsive" src="http://placehold.it/350x200"alt="">
        <div class="overlay">
            <div class="btn-group" data-toggle="buttons">
                <label class="btn btn-primary cke">
                    <input type="checkbox" value="nao"><i class="fa fa-heart"></i>
                </label>
            </div>
        </div>
    </div>
</div>

JavaScript

$('input[type=checkbox]').change(function(){
    if($(this.checked)){
       $(".abcd").append('<img id="hea" class="img-responsive" src="../images/heart.png">');
       return;
    }
});

With this code when I click on checkbox it adds the img of append to all span with class abcd and I'd like it to add just span next to checkbox that was clicked.

    
asked by anonymous 21.06.2016 / 21:09

1 answer

1

You can use this:

$(this).closest('.hovereffect').find(".abcd").append(

In the background, you have as a starting point the this , go up in the DOM and look for the div with class .hovereffect and then go down with .find(".abcd") , and that's your span ...

    
21.06.2016 / 21:32