in Education by
I'm trying to assemble and display image slices which have been preloaded into a two dimensional array, but the code is only showing placeholders instead of the images. The image slices have been preloaded with a javascript function, but when I use a second javascript function to retrieve and assemble the image slices I'm just getting placeholders instead of the images. The first javascript function to preload the image slices is called at the bottom of the head section in the HTML page and uses a query string to retrieve part of the image file name (queryData[2]), the second javascript function to assemble the images is in the body tag and called as onload=assembleImage();. I've used console.log in both functions which have shown the image slices are present. Is there a reason why I'm only getting placeholders instead of images? My code is below. // First function to preload image slices into a two dimensional array. function preloadImages(){ preloadImages = []; var row = 5; var col = 5; for (var r = 0; r < row; r++){ preloadImages[r] = new Image(); for (var c = 0; c < col; c++){ preloadImages[r][c] = new Image(); preloadImages[r][c].src = queryData[2] + "_" + (r) + "_" + (c) + ".jpg"; } } //console.log(preloadImages[r][c]); } // Second function to retrieve and assemble image slices. function assembleImage(){ var row = 5; var col = 5; var assembleImage = document.getElementById("prodImage"); var imageTable = " "; for (var r = 0; r < row; r++){ imageTable += " "; for (var c = 0; c < col; c++){ imageTable += " "; console.log(preloadImages[r][c]) } imageTable += ""; } imageTable += "
"; imageTable += "image"; imageTable += "
"; assembleImage.innerHTML = imageTable; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
One of the main issues you are hitting, is assigning preloadImages[r] to a new Image(), rather than an Array. Also, you define preloadImages as a function and a variable (implicit global) to use for your two-dimensional Array. After renaming those variables and doing a bit of cleanup, your code runs fine, as seen below: document.addEventListener('DOMContentLoaded', function () { var queryData = [ 'https://makelin.us/100/100', 'https://makelin.us/100/100', 'https://makelin.us/100/100' ]; var preloadImages = []; // First function to preload image slices into a two dimensional array. function preloadAllImages() { var row = 5; var col = 5; for (var r = 0; r < row; r++) { preloadImages[r] = []; for (var c = 0; c < col; c++) { preloadImages[r][c] = new Image(); preloadImages[r][c].src = queryData[2]; } } // console.log(preloadImages); } preloadAllImages(); // Second function to retrieve and assemble image slices. function assembleImage() { var row = 5; var col = 5; var assembleImage = document.getElementById("prodImage"); var imageTable = " "; for (var r = 0; r < row; r++) { imageTable += " "; for (var c = 0; c < col; c++) { imageTable += " "; // console.log(preloadImages[r][c]) } imageTable += ""; } imageTable += "
"; imageTable += "image"; imageTable += "
"; // console.log(imageTable); assembleImage.innerHTML = imageTable; // console.log(assembleImage); } assembleImage(); }); <!DOCTYPE html> repl.it
Run code snippetExpand snippet

Related questions

0 votes
    Consider the following code. What is a good hashing function for the array in Key to be used in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am working on a project where I need to use array values in the getElementById() in javascript. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I am attempting to solve a problem where if the elements in one array are squared, and all of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    Angular js Image upload how to check width and height example my withd=200 and heidht =200 if give ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am trying to change an image depending on what option is selected in a drop down menu. I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I am trying to implement an algorithm, that finds the shortest path in the following two dimensional array ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I am trying to sort an array of objects by the name field but the ordering is wrong. The order I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Series is a one-dimensional labeled array capable of holding any data type. (a) True (b) False The ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    I was trying to write a quick-and-dirty script to generate plots easily. For that, I was using the following code ( ... do I do that? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    So basically this is what I want to do: There's a gap in the x axis between week 27 and week ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
...