Changing the image source using jQuery

You can use jQuery’s attr() function. For example, if your img tag has an id attribute of ‘my_image’, you would do this:

<img id="my_image" src="first.jpg" alt="Insert link 1 alt text here" />

Then you can change the src of your image with jQuery like this:


To attach this to a click event, you could write:

    'click': function(){

To rotate the image, you could do this:

    'click': function() {
         var src = ($(this).attr('src') === "")
            ? 'img2_on.jpg'
            : "";
         $(this).attr('src', src);

Leave a Comment