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');
} });
)🛠️ Related Fixes
How to Fix: Stuck in tutorial hell after 4 years: How do I b
Learn to build websites and think independently with coding skills.
How to Fix: Trying to sync mutliple audio tracks to a movie
Complex audio track synchronization can be challenging due to the larg
How to Fix: Failed to merge latest branches from upstream re
Update local repository with latest upstream branches.