javascript - apply class to one ajax loaded img if that img src is currently present on page? -
here's question: have list of images loaded via .load( onto div on page
$('.widget-top').live("click", function() { $(".area").load("/galleries/ #projects > li a"); });
they load structure:
<a href="#"><img src="photoa.jpg" /> </a> <a href="#"><img src="photob.jpg" /> </a> <a href="#"><img src="photoc.jpg" /> </a> <a href="#"><img src="photod.jpg" /> </a> <a href="#"><img src="photoe.jpg" /> </a>
i click on image , src retrived , added form value so:
$("#widgets-right .area a").live("click", function() { $(this).toggleclass('selected'); if ( === 2){ alert('added project 1 if want add more projects click button below'); var title = $(this).attr('title'); $(".title").val(title); var link = $(this).attr('href'); $(".link").val(link); var img = $("img", this).attr('src'); $(".img").val(img); var imgexample = $("img", this).attr('src'); $(".gallery_one").attr("src", imgexample); } else{ --; alert('added project'+i); var title = $(this).attr('title'); $('.title'+i).val(title); var link = $(this).attr('href'); $('.link'+i).val(link); var img = $('img', this).attr('src'); $('.img'+i).val(img); var imgexample = $('img', this).attr('src'); $('.gallery_one'+i).attr("src", imgexample); i++; } });
and can see line $(this).toggleclass('selected');
it's toggling class when clicked. here good, problem occurs when save form , get's refreshed via ajax. images selected class no longer have selected class because have been refreshed. have input fields current src's of selected images on page. there way search through fields , reapply class if src'sin input fields match src's of images reloaded via .load() call? chris
you need store selected item in variable , after images loaded need assign class back
var selected; // should in context shared between both methods $("#widgets-right .area a").live("click", function() { $(this).toggleclass('selected'); if ( === 2){ alert('added project 1 if want add more projects click button below'); var title = $(this).attr('title'); $(".title").val(title); var link = $(this).attr('href'); $(".link").val(link); var img = $("img", this).attr('src'); $(".img").val(img); selected = img; var imgexample = $("img", this).attr('src'); $(".gallery_one").attr("src", imgexample); } else{ --; alert('added project'+i); var title = $(this).attr('title'); $('.title'+i).val(title); var link = $(this).attr('href'); $('.link'+i).val(link); var img = $('img', this).attr('src'); $('.img'+i).val(img); selected = img; var imgexample = $('img', this).attr('src'); $('.gallery_one'+i).attr("src", imgexample); ++; } }); $('.widget-top').live("click", function() { $(".area").load("/galleries/ #projects > li a", function(){ if(selected){ $(".area").find('img[src="' + selected + '"]').closest('a').toggleclass('selected'); } }); });
Comments
Post a Comment