Changing the image source using jQuery

You can use jQuery's attr() function. For example, if you img tag has an id attribute of 'my_image':

<imgid="my_image"src="first.jpg"/>

Then you can change the src in jQuery by:

$("#my_image").attr("src","second.jpg");

To attach this to an onclick event, you could write:

$('#my_image').on({'click':function(){
        $('#my_image').attr('src','second.jpg');}});

To rotate the image, you could do this:

$('img').on({'click':function(){var src =($(this).attr('src')==='img1_on.jpg')?'img2_on.jpg':'img1_on.jpg';
         $(this).attr('src', src);}});

Comments (1) -

Anya Mintreas
Anya Mintreas
12/14/2013 12:18:02 AM #

Good work Smile cheers!

Add comment

biuquote
  • Comment
  • Preview
Loading

Sponsored by

Calendar

<<  August 2018  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

RecentComments

TextBox