javascript - Replace an image at the DOM -
i looking replace particular image when click on , change active image. click on other image, clicked image becomes active , other image becomes inactive again.
the html markup looks this:
the html using is:
<ul id="weeklyprizeblockthumb"> <li class="active"> <img src="images/bts/bts_overlay_wp_box_thumbw1.jpg" alt="week1" id="week1" /> <p class="text"> <span>gearing school:</span> <span>$100 of mead® school supplies!1</span></p> </li> <li> <img src="images/bts/bts_overlay_wp_box_thumbw2.jpg" alt="week2" id="week2" /> <p class="text"> <span>sticking schedule:</span> <span>$100 gift card container store®!</span></p> </li> <li> <img src="images/bts/bts_overlay_wp_box_thumbw3.jpg" alt="week3" id="week3" /> <p class="text"> <span>doing lunch:</span> <span>soft lunch bag $100 of unilever products!</span></p> </li> </ul>
the js is:
var src1 = $('ul#weeklyprizeblockthumb li').find('img').attr("src").replace("_active.jpg", ".jpg"); $('ul#weeklyprizeblockthumb li').find('img').attr("src", src1); //$('#overlay_wp_weekimg div').find('img').attr("src").replace("_active", ""); var src = $(this).find('img').attr("src").match(/[^\.]+/) + "_active.jpg"; $(this).find('img').attr("src", src);
this not working correctly. not de-active-ate previous images.
try this~
$(function(){ $("#weeklyprizeblockthumb li").each(function(){ var li = $(this); li.click(function(){ $("#weeklyprizeblockthumb>li>img").each(function(index){ $(this).attr("src","images/bts/bts_overlay_wp_box_thumbw"+ (index + 1) +".jpg"); alert($(this).attr("src")); }) li.find("img").attr("src","images/bts/_active.jpg"); alert(li.find("img").attr("src")); }) }) })
Comments
Post a Comment