javascript - How to trigger an element within the same container using jQuery -


i trying create site dynamically produces code below. has multiple instances of div.container , children. problem when click on thumbnails(.color-thumbs img), triggers change on images(.imgwrapper img). need create unique trigger thumbnails affect image within same container.

here code:

$('.color-thumbs img').click(function() {    var thmb = this;    var src = this.src;    $('.color-thumbs img').parent('.imgwrapper').append('crayz');      $('.imgwrapper img').fadeout(400,function(){        thmb.src = this.src;        $(this).fadein(400)[0].src = src;    });  });
.color-thumbs img{    margin-right:3px;    margin-top:3px;    width:30px;    height: 30px;    float:left;      box-sizing: border-box;  }    .container {    clear: both;    margin-bottom: 4em;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <div class="container">    <div class="imgwrapper">      <img src="http://placehold.it/300x160/cf5" />    </div>      <div class="color-thumbs">      <img src="http://placehold.it/300x160/444" />      <img src="http://placehold.it/300x160/f1f" />    </div>  </div>    <div class="container">    <div class="imgwrapper">      <img src="http://placehold.it/300x160/cf5" />    </div>      <div class="color-thumbs">      <img src="http://placehold.it/300x160/444" />      <img src="http://placehold.it/300x160/f1f" />    </div>  </div>

thanks in advance!

you can use .closest() fine container element clicked img belongs use .find() find target .imgwrapper img element inside like

$('.color-thumbs img').click(function() {    var thmb = this;    var src = this.src;      $(this).closest('.container').find('.imgwrapper img').stop().fadeout(400, function() {      $(this).fadein(400)[0].src = src;    });  });
.color-thumbs img {    margin-right: 3px;    margin-top: 3px;    width: 30px;    height: 30px;    float: left;    box-sizing: border-box;  }  .container {    clear: both;    margin-bottom: 4em;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <div class="container">    <div class="imgwrapper">      <img src="http://placehold.it/300x160/cf5" />    </div>      <div class="color-thumbs">      <img src="http://placehold.it/300x160/444" />      <img src="http://placehold.it/300x160/f1f" />    </div>  </div>    <div class="container">    <div class="imgwrapper">      <img src="http://placehold.it/300x160/cf5" />    </div>      <div class="color-thumbs">      <img src="http://placehold.it/300x160/444" />      <img src="http://placehold.it/300x160/f1f" />    </div>  </div>


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -