2

I had a question about loading images into an array in JS and then appending them to various places to your website.

Let's say I have a folder with 3 images that I'd like to load into an array and then place in this setting

 <body> 

    <div id = "mainImage1"></div>             
    <div id = "mainImage2"></div>             
    <div id = "mainImage3"></div>             
</body>

Is it possible to load the images, first into a general array, and then put those images within their divs?

Kind of like this? (This code is incorrect)

var mainImage1 = document.getElementById("mainImage1");
var mainImage2 = document.getElementById("mainImage2");
var mainImage3 = document.getElementById("mainImage3");

var mainImageSelection = new Image();

mainImageSelection.src = "Images/" + mainImageSelection[i];

mainImage1.appendChild(mainImageSelection[0]);
mainImage2.appendChild(mainImageSelection[1]);
4
  • do you want to move the images around DOM or load in js and then attach? Commented Mar 21, 2016 at 14:00
  • Are the image names always the same, or can they change? Is there a relationship between imageName and divName? Commented Mar 21, 2016 at 14:02
  • I was trying first just to load in JS and then attach to various parts of the website (I had created just some rudimentary css to move the divs to different parts of the page) Commented Mar 21, 2016 at 14:04
  • You need to use document.createElement('img'); Commented Mar 21, 2016 at 14:04

4 Answers 4

1

More dynamic way to do it:

var mainImage1 = document.getElementById("mainImage1");
var mainImage2 = document.getElementById("mainImage2");
var mainImage3 = document.getElementById("mainImage3");
var myArr = [mainImage1,mainImage2,mainImage3];
var images = ["https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg","https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg","https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"]

for(var i=0;i<myArr.length;i++){
var mainImageSelection = new Image();
    mainImageSelection.src = images[i];
    var div = myArr[i];
    div.appendChild(mainImageSelection);
};

fiddle [example] https://jsfiddle.net/h1t4vxy6/1/

Sign up to request clarification or add additional context in comments.

5 Comments

That's a wonderfully elegant solution. QQ: the last line: div.appendChild(mainImageSelection); If I have the divs in various sections of the website and their placement controlled by css can I fit them to whatever div I want?
exactly @TheodoreSteiner
Thank you so much!
One last thing, you create a separate div at the very end var div=myArr[i] and I get that is holding the array images itself, but then if let's say I wanted to attach image[0] to the mainImage1 Div would i type it out: mainImage1.appendChild(mainImageSelection[0])
I think I've made a booboo somewhere, can you take a peek? jsfiddle.net/theodore_steiner/e92eL0eb
1

Just place the following code in a loop, updating the index and div id:

var elem = document.createElement("img");
elem.setAttribute("src", "Images/" + mainImageSelection[0]);
elem.setAttribute("height", "768");
elem.setAttribute("width", "1024");
elem.setAttribute("alt", "Flower");
document.getElementById("mainImage1").appendChild(elem);

Comments

0

var imgArr = [];

var myImg = new Image();
myImg.src = "http://jstsolutions.net/wp-content/themes/realty/lib/images/key_img2.png";
imgArr.push(myImg);

$("body").append(imgArr[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

Here is a solution with a common 'onload' implementation. This way you can add all images at once to the dom, when all of them are loaded. This is a cleaner way to manipulate the dom.

var imageUrls = ['https://lh3.googleusercontent.com/MiUKb7zmiGyTbZDYl-GcqhwwgYKQTef6GlbqD8M09Iee4HKo-eZ_elDaDZWIjPt-QP6JdKmZb9gDlMd_rgOBsoKgdzpHneXqTA0IrSDl', 'https://lh3.googleusercontent.com/1I-SpxfmclQuLzrEeuVSskbwgcgDKrlTex_Lr7P9w_jpyyz-cblCIWhBaaotiTtZER30AheCW7TZLJebadC39_pCgxzcWQnwTQf01Gtz', 'https://lh3.googleusercontent.com/q9RXNZmgMHFxFCPX9iweCHhQsacan7g0_bswUe_GOy0W53iSeOxnLXcGkGUWPyR5K-qQBv7MtHEm-Ddzl0ja9C7L790P67LhTjGDJso'];
var images = [];

var loadedImages = 0;

function onLoad() {
  loadedImages++;
  if (loadedImages === imageUrls.length) {
    onAllImagesLoaded();
  }
}

function onAllImagesLoaded() {
  var imagesContainer = $('.images');
  images.forEach(function(element, index) {
    imagesContainer.append(element);
  });
}

imageUrls.forEach(function(element, index) {
  var image = new Image();
  image.src = element;
  image.onload = onLoad;
  images.push(image);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="images">

</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.