Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change <img /> image with JavaScript Image object

I have a JavaScript Image object that I load dynamically with jQuery.

What I want to do is change a <img /> image with one stored on my Image object. How should I do this?

Note: I want to avoid changing the source of my <img /> tag as it downloads it again from the server, and I already have the image stored in my image object

like image 935
Pacha Avatar asked Mar 10 '13 21:03

Pacha


People also ask

How do I change an image using JavaScript?

JavaScript provides a src attribute to change the image source by specifying the path of the file. For instance, the getElementId() method is utilized to extract the HTML element through id, and then the src property will change the source image. After extraction, the new source image file is assigned.

What is image () in JavaScript?

Image() The Image() constructor creates a new HTMLImageElement instance. It is functionally equivalent to document. createElement('img') .

How can I swap two images in JavaScript?

Hello guys, in this post we will explore how to find the solution to How To Swap Two Images In Javascript in programming. <img src="ble. jpg" id="F" data-value="" onclick="swap(5)" /> <img src="ilk. jpg" id="4" data-value="2" onclick="swap(6)"/> var img_1_value = document.


3 Answers

You mean

$('#imageToChange').replaceWith(imageObject)

?

like image 190
mplungjan Avatar answered Oct 05 '22 13:10

mplungjan


New Image object:

var Image_off = new Image();
Image_off.src = 'first.jpg';

image src change with jQuery:

$("#my_image").attr("src",Image_off.src);
like image 20
Pro.Ton Avatar answered Oct 05 '22 12:10

Pro.Ton


With jQuery... have both images already on your page and show or hide either one, based on a logical condition.

like image 32
klewis Avatar answered Oct 05 '22 12:10

klewis