Coding⏱️ 1 min read📅 2026-05-30

How to Fix: Changing the image source using jQuery

Change image source using jQuery

Quick Answer: Use jQuery's click event to change the image src attribute.

When someone clicks on an image, you can change the image src using jQuery. Here's how:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

$(document).ready(function(){
'$'.each(function() {
'$'.on('click', function(){
var imgSrc = $(this).attr('src');
if (imgSrc.includes('img1_on.gif')) {
$(this).attr('src', 'img1_off.gif');
} else if (imgSrc.includes('img2_on.gif')) {
$(this).attr('src', 'img2_off.gif');
} });
'$'.on('click', 'a img', function(){
var imgSrc = $(this).attr('src');
if (imgSrc.includes('img1_on.gif')) {
$(this).attr('src', 'img1_off.gif');
} else if (imgSrc.includes('img2_on.gif')) {
$(this).attr('src', 'img2_off.gif');
} });
)

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database