javascript - Jquery .each, but only that current element -


i'm using following script

$('.cat_glass_thumbs ul li').each(function(){     $(this).click(function(){         orig_src = $(this).attr('data-orig');         $('.cat_glass_thumbs img').attr('src', orig_src);     }); }); 

i have loop outputs multiple divs, of them same structure, different content.

right jquery works ".cat_glass_thumbs" div, if flick on li, update src in every .cat_glass_thumb, instead of updating 1 in.

tips/pointers appreciated

html of each reference:

<div class="cat_glass_thumbs">        <a href="" title="black/silver" class="product-image thumbnail">           <span class="main_image">                <img src="t/2064-a-blk-slv-52-angle.jpg" alt="black/silver">           </span>       </a>          <ul id="thumbs_container">             <li class="img_thumbs" data-orig="/t/-2064-a-blk-slv-52-angle.jpg">             </li>             <li class="img_thumbs" data-orig="/t/-2064-a-blk-slv-52-front.jpg">             </li>              <li class="img_thumbs" data-orig="/t/-2064-b-blk-gld-52-angle.jpg">              </li>         </ul>       </div> 

drop each, it's useless in case (jquery automatically you):

$('.cat_glass_thumbs ul li').click(function(){     var orig_src = $(this).data('orig');     $('a img', this).attr('src', orig_src); }); 

js fiddle demo

note used $('a img', this) in form of $( selector [, context ] ) described in jquery documentation.


edit

your js code alone made me think of html structure. fit 1 posted, need target image in parent .cat_glass_thumbs using $.closest():

$('.cat_glass_thumbs ul li').click(function(){      var orig_src = $(this).data('orig');     $('a img', $(this).closest('.cat_glass_thumbs')).attr('src', orig_src); }); 

updated js fiddle

$.closest()

description: each element in set, first element matches selector testing element , traversing through ancestors in dom tree.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -