I have an if statment that’s not working correctly and I believe it’s because of the use of ‘this’ but I’m not sure how to fix it. This is the code:
$('.enlarge').click(function() {
var id = $(this).find(".enlarged_txt").attr('id');
$('#full_image').animate({
height: "100%"
}, 300, function() {
if ( $(this).hasClass("v") ) {
$('#full_image img').attr('src','http://www.klossal.com/klossviolins/instruments/violins/full/' + id + '.jpg');
fadeIn($('#full_image img'));
$("#close_2").css({
display: "block"
});
$("#close").css({
display: "block"
});
}
});
});
<div class="enlarge v" style="float:right;margin-right:70px;margin-top:5px;">
<img class="enlarged_unselected" style="float:left;margin-top:6px;" src="http://www.klossal.com/klossviolins/elements/fullscreen_unselected.png"/>
<img class="enlarged_selected" style="float:left;display:none;" src="http://www.klossal.com/klossviolins/elements/fullscreen_selected.png"/>
<div id="ChasHunnicutt_1928" style="float:left;padding-left:8px;" class="enlarged_txt">Enlarge Image</div>
</div>
Yes, there is a problem with
thisin this.Because the second time you use
thisyou use it in a new function inside theanimate(call. This time you usethisthethisreferences (according to this jQuery doc) the “DOM element being animated.”If you want to reference the original
thispassed in by the .click(handler to your top level function (which references the DOM element being clicked) you need to save it first, then replace the secondthiswith that saved reference to the originalthis.Keywords are fun.
Like so :
Which should fix the problem with
this.