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 :: import npm module node.js 
Javascript :: smtp js 
Javascript :: decimal to base 32 javascript 
Javascript :: node-schedule job on specific datetime 
Javascript :: how to concatenate a string in javascript 
Javascript :: function declaration and function definition in javascript 
Javascript :: js sum digits 
Javascript :: adding logo to vscode extension development 
Javascript :: how to return argument in javascript 
Javascript :: send data using fetch 
Javascript :: adding a if stement in jsx 
Javascript :: simplexml format xml 
Javascript :: remove an element from array javascript 
Javascript :: how to get the value of textarea in react 
Javascript :: how to find last element of an array 
Javascript :: mysql json 
Javascript :: loop an array javascript 
Javascript :: how to create request body javascript 
Javascript :: set id to div element in Javascript 
Javascript :: enforcefocus select2 modal 
Javascript :: Factorial Recursion Function in Javascript 
Javascript :: return inside for loop javascript 
Javascript :: Adding A forEach Function To An HTMLCollection JavaScript 
Javascript :: super method in js 
Javascript :: selected dropdown value 
Javascript :: monaco editor get content 
Javascript :: how to find a name of class from page in jquery 
Javascript :: get last element in array in javascript 
Javascript :: set property js 
Javascript :: queryselectors select element whole class 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =