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

<<  November 2018  >>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

View posts in large calendar

RecentComments

TextBox