Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:22:24+00:00 2026-06-15T19:22:24+00:00

This topic has been on my mind the last little while and I have

  • 0

This topic has been on my mind the last little while and I have been thinking about the efficiency for DOM updates relating to replacing / updating images. I haven’t been able to find a discussion on this specifically and I am not even sure if there is an ideal solution between the two options I’ll show below.

The best answer I’ve come up with is that is it totally dependent on if you need to update additional attributes on the <img> tag like the alt, width, height values if they are being used.

Ok, so say you want to update an image or do an image swap between two images, what do you believe is the most efficient or elegant way. Is it better to replace the <img> element completely with a new <img> element with an updated src or to only update the existing <img>‘s src value with the new image?

The example I have use small images, would performance be effected by image size or whatever.

The two ways I have are:

$('.swapimage_ele').on("click", function(evt){
    var img1 = $("#img1").html();
    var img2 = $("#img2").html();

    $("#img1").html(img2);
    $("#img2").html(img1);
});

$('.swapimage_src').on("click", function(evt){
    var img1 = $("#img1 img").attr("src");
    var img2 = $("#img2 img").attr("src");

    $("#img1 img").attr("src", img2);
    $("#img2 img").attr("src", img1);
});

​
The quick working example I’ve wrote showing the two options is here: http://jsfiddle.net/jshaw/KMmeR/

Just curious on everyone’s opinion or preference and if there’s an ideal way of doing this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:22:26+00:00Added an answer on June 15, 2026 at 7:22 pm

    Less jQuery, and definitely not using .html()

    $('.swapimage_src').on("click", function(evt){
        var img1 = document.querySelector("#img1 img");
        var img2 = document.querySelector("#img2 img");
    
        var temp = img1.src;
        img1.src = img2.src;
        img2.src = temp;
    });
    

    Or if you must support IE6/7:

    $('.swapimage_src').on("click", function(evt){
        var img1 = document.getElementById("img1").getElementsByTagName("img")[0];
        var img2 = document.getElementById("img2").getElementsByTagName("img")[0];
    
        var temp = img1.src;
        img1.src = img2.src;
        img2.src = temp;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about regex/Python. Sorry if this topic has been discussed millions
I know this topic has been beaten to death a little on SO, but
I am aware this topic has been raised severals, however i have a slight
This topic has been discussed but I've been unsuccessful. I have a basic mesh
I know this topic has been discussed a lot , but I have a
I know this topic has been pounded as I have read gillions of posts
This topic has been covered on this forums, but I haven't found a working
This topic has been scratched once or twice, but I am still puzzled. And
I know this topic has been discussed but I think it has some differences.
This is my first post. I know this topic has been discussed before in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.