Coding⏱️ 1 min read📅 2026-06-02
How to Fix: Changing the image source using jQuery
Fix Changing the image source using jQuery. Use jQuery's click event to change the i. Step-by-step guide included.
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
Fix Stuck in tutorial hell after 4 years: How do I bui. Practice build
How to Fix: Trying to sync mutliple audio tracks to a movie
Fix Trying to sync mutliple audio tracks to a movie bu. Consider using
How to Fix: Failed to merge latest branches from upstream re
Fix Failed to merge latest branches from upstream repo. Try running 'g