Change an image src with jQuery
Codemia Knowledge Hub ·
To change an image source with jQuery, update its src attribute with .attr(): $(img).attr('src', newUrl). The browser starts loading the new image as soon as the attribute changes. In real code you may also want to handle load errors, preload large images, and watch out for caching.
Basic swap with .attr()
This is the core technique: select the image, then set a new value for its src attribute, here in a click handler.
Cycling through several images
Galleries, theme previews and product selectors switch between several images. That is just repeated src updates:
Falling back when the image fails to load
If the new path is wrong, changing src leaves a broken image on screen. Handle the image's error event to swap in a safe default, especially when the source may be unreliable:
Preloading before the swap
Swapping in a large image instantly can cause visible flicker or layout shifts. Preload it first and change src only once it has loaded:
This helps with carousels and hero images, where users should not see a half-loaded transition. The onerror handler also gives you a place to log or recover when the new image cannot be loaded, instead of leaving the old one in place silently.
When the image seems not to change
If the URL stays the same, the browser may serve a cached copy, which looks as though the code did nothing. A cache-busting query string forces a fresh request:
Do not apply this blindly to static assets in production, but it is useful for images that are regenerated often, such as charts or screenshots.
Doing it without jQuery
If the project already uses jQuery, .attr() is fine. If it does not, loading jQuery for a single attribute update is unnecessary, since plain DOM code is just as short:
Related reading
- Check if a variable is of function type
- Check if an array contains any element of another array in JavaScript
- Check if an element contains a class in JavaScript?
- Check if any pending/running promises exist anywhere in your entire Node.js process without having access to promise variables themselves
- Check if checkbox is checked with jQuery
- Check if element exists in jQuery
- Check if item is in an array / list
- Check synchronously if file/directory exists in Node.js
System design interview problems on this topic
- Design Google Doc: solution and walkthrough
- Design Facebook Messenger: solution and walkthrough
- Design Typeahead Suggestion: solution and walkthrough
- Design an Online Presence Indicator Service: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.