Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js load multiple images

// Array of images to load.
const images = [
	"path/to/image.extension",
	"/images/someimage.png",
	"./images/anotherimage.jpg",
	"../separatefolder/assets/onelastimage.gif"
];

// Function to load an image.
function loadImage(src) {
	return new Promise(function(res, rej) {
		const image = new Image();
		function loadCallback() {
			image.removeEventListener("load", loadCallback);
			image.removeEventListener("error", errorCallback);
			res(image);
		}
		function errorCallback() {
			image.removeEventListener("load", loadCallback);
			image.removeEventListener("error", errorCallback);
			rej(image);
		}
		image.addEventListener("load", loadCallback);
		image.addEventListener("error", errorCallback);
		image.src = src;
	});
}

// Function to load multiple images.
function loadImages(...srcs) {
	const promises = [];
	for (let i = 0; i < srcs.length; i++) {
		promises.push(loadImage(srcs[i]));
	}
	return Promise.all(promises);
}

// Load the images in the images array and print them to the console.
loadImages(...images).then(console.log);
Comment

create multiple images in js

for(let i = 0; i < 10; i++) {
   const img = document.createElement("img");
   img.src = "https://picsum.photos/200/301";
}
Comment

add multiple images inside the DOM js


function placeImage(x)
{
    var div = document.getElementById("div_picture_right");

    div.innerHTML = ""; // clear images

    for (counter=1;counter<=x;counter++) {
        var imagem=document.createElement("img");
        imagem.src="borboleta/Borboleta"+counter+".png";
        div.appendChild(imagem);
    }
}

window.onload = function() {
    placeImage(48);
};

Comment

add multiple images inside the DOM js

function placeImage(x)
{
    var div = document.getElementById("div_picture_right");

    div.innerHTML = ""; // clear images

    for (counter=1;counter<=x;counter++) {
        var imagem=document.createElement("img");
        imagem.src="borboleta/Borboleta"+counter+".png";
        div.appendChild(imagem);
    }
}

window.onload = function() {
    placeImage(48);
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: get current directory vbscript 
Javascript :: js set url params 
Javascript :: loopback upsert with where 
Javascript :: get middle of string js 
Javascript :: javascript substring after character 
Javascript :: express search query template 
Javascript :: react native android run 
Javascript :: how To clear all the input element inside div using jquery 
Javascript :: js multiply string 
Javascript :: javascript open new window 
Javascript :: how to get clicked element class in jquery 
Javascript :: asp.net core 3.1 convert system.string[] to javascript 
Javascript :: copy to clipboard javascript 
Javascript :: convert iso 8601 to utc javascript 
Javascript :: sequelize limit 
Javascript :: seleccionar value select2 js 
Javascript :: jquery modify style attribute 
Javascript :: rgb to hex js 
Javascript :: jquery get unique values from array 
Javascript :: javascript check if elements of one array are in another 
Javascript :: how to set state when change viewport react 
Javascript :: How to make "select option" select option for quantity 
Javascript :: js change button text 
Javascript :: javascript remove last character in a string 
Javascript :: get child eleemtn by native element angular 
Javascript :: use regex to get urls from string 
Javascript :: how to loop and add number in fuction for javascript 
Javascript :: ciclo for javascript 
Javascript :: responsive grid using antd 
Javascript :: javascript forever loop 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =