I wrote some code to toggle the src of an image and also to pause/resume the jQuery cycle2 plugin.
I'm not sure why it isn't working and would appreciate some help.
$('.play-pause').click(function(){
if ($(this).attr('src', '/images/template/pause.png')) {
$(this).attr('src', '/images/template/play.png');
$('.cycle-slideshow').cycle('pause');
} else if ($(this).attr('src', '/images/template/play.png')) {
$(this).attr('src', '/images/template/pause.png');
$('.cycle-slideshow').cycle('resume');
}
});
If I remove the 'else if' statement the first 'if' statement works.
Thanks for the help.
Jeff
I know this is a late answer but what about using something simplier like:
Be more generic, and check the source for the image only, not the entire string :
For UI elements, such as pause/play buttons, you should be using CSS backgrounds, not inline images. That was you can simply swap class names to change the image.
You can use inline images, but you should simplify using class names, once again.
Even more reducing lines is possible but avoided confusion. Basically , it gets the current state attribute source and checks and assigns required source.
Occasionally it actually is reasonable to want to swap the image src directly rather than handle it in CSS (working around weird bugs, mostly). What I've done in this case is written a simple helper function that toggles between the initial src and an alternate src that you can also specify on the image element itself, like so:
and the image element itself just has an extra property called 'toggle_src' (or you could add it dynamically if you needed to):
This is another approach:
And for the jQuery
Needs some fun animation, probably, but gets the work done.
Here's a snippet: