October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

Get an Image by ID and Change Its src in JavaScript

Use getElementById() to find one image and assign a new src. For multiple images, select the intended group and update each element.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use document.getElementById() to retrieve one uniquely identified image, then assign its new path to src. Check that the element exists first:

const img = document.getElementById("hero");
if (img) {
  img.src = "new-image.jpg";
}

Change the source of one image

Give the image an ID in your HTML, then use that exact ID in JavaScript:

<img id="hero" src="old-image.jpg" alt="A view of the mountains">

<script>
  const img = document.getElementById("hero");
  if (img) {
    img.src = "new-image.jpg";
  }
</script>

getElementById() returns the element whose ID matches the string you pass. The MDN reference documents this lookup method. An image element exposes its source through the src property; see MDN’s HTMLImageElement reference.

Make sure the lookup matches

  • IDs should be unique in the document, so an ID lookup is for one element, not a group.
  • The ID string is case-sensitive: "hero" does not match id="Hero".
  • The method name is exactly getElementById. If there is no match, it returns null; the existence check prevents trying to set src on a missing element.

Apply the change to several images

getElementById() is not a multi-element selector. If several images should receive the same source, give them a shared class and use querySelectorAll() to select and iterate over that set:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img class="gallery-image" src="one.jpg" alt="First view">
<img class="gallery-image" src="two.jpg" alt="Second view">

<script>
  document.querySelectorAll("img.gallery-image").forEach((img) => {
    img.src = "new-image.jpg";
  });
</script>

Choose a selector that identifies only the images you intend to change. MDN describes DOM selection and traversal methods, including selectors that can return multiple matches.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Review the alternative text

Keep the alt text useful: it matters when an image cannot be displayed. If the replacement changes what the image communicates, update the alternative text to match. See MDN’s <img> reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.