samedi 21 janvier 2017

jquery onclick toggle image src

I put together this code here:

 HTML:
 <a href="#"><img id="img1" src="memes/2a.png" height="500" width="600"></a> 

 JS:  
            $('img').on({
                'click': function() {
                     var src = ($(this).attr('src') === 'memes/2a.png')
                         ? 'memes/2b.png'
                         : 'memes/2a.png';
                     $(this).attr('src', src);
                 }
            });

It should toggle between the two images: 2a.png and 2b.png it nothing happens when i click on it.

Can someone tell me what i'm doing wrong?




Aucun commentaire:

Enregistrer un commentaire