I have images with the class name ".mute_btn", and when I click on them, the image source changes:
$('.mute_btn').toggle(function () { var clicked = false; var src = $(this).attr("src"); src = src.replace(/(.*)\.(png|gif|jpg|jpeg)$/, "$1-over.$2"); $(this).attr("src", src); }, function () { var src = $(this).attr("src"); src = src.replace(/(.*)-over\.(png|gif|jpg|jpeg)$/, "$1.$2"); $(this).attr("src", src); });
But I saw toggle () deprecated in jQuery 1.8
So I'm trying to do it like this:
var clicked = false; $('.mute_btn').click(function() { if (clicked) { var src = $(this).attr("src"); src = src.replace(/(.*)-over\.(png|gif|jpg|jpeg)$/, "$1.$2"); $(this).attr("src", src); clicked = false; } else { var src = $(this).attr("src"); src = src.replace(/(.*)\.(png|gif|jpg|jpeg)$/, "$1-over.$2"); $(this).attr("src", src); clicked = true; }});
But the result is not perfect. Once upon a time, images do not change.
Do you know what is wrong?
source share