javascript - jQuery image swap if-else statement not working -
i have problem jquery.
i have following code inside $(document).ready(function() {...})
$("#one_div").hide(); $('#button').click(function(){ if (this.src == 'img/img1.png'){ this.src = 'img/img2.png'; } else { this.src = 'img/img1.png'; } $("#one_div").slidetoggle(800); });
#button
id of image. one_div
id of div.
actually, clicking on image toggles div, problem image swapped once, img1
img2
, , never switched back. did wrong?
put code inside of $(window).load(function() { ... });
instead sure images loaded before toggling them.
$(document).ready(function() { $("#one_div").hide(); }); $(window).load(function() { $('#button').click(function(){ if ($(this).attr('src') == 'img/img1.png'){ $(this).attr('src', 'img/img2.png'); } else { $(this).attr('src', 'img/img1.png'); } $("#one_div").slidetoggle(800); }); });
Comments
Post a Comment