How to dynamically load images with HTML5 and Javascript?

baqai

Well-known member
Dec 9, 2008
1,711
0
41
47
Karachi
www.saquibbaqai.com
Hey all you programming cheeeta's

i am stuck with a problem and i can't get my head around it, i am making a html5/css/java based page in which user is presented with dynamic drop down menus, each menu apart from the very first is populated dynamically depending on the choice made on the previous one

e.g.

Select Game (list of games) -> loads list of game -> User selects Dota 2 -> loads list of categories like strength/agility/intelligence -> User selects a category e.g. Strength -> loads list of strength heros

all this is being done via function which loops through array and with if condition

now i am stuck, i want to load an image of the hero when it's selected, i also want the html to be without the image when nothing is selected, also i want to load text from a text file which can be image description or caption and it also needs to change with the image

heroname.innerHTML=""; <- this sorts out my problem of making text/label empty when ever change is make

A select function i am making to populate the list is as follows

<select id="gamelist" name="game-name" onchange="populate(this.id,'herolist')">

gamelist would be the id while game-name would be value, this.id will pass the value gamelist to the function "populate" while herolist is the id for the next drop down menu

i am lost in what to do, i cannot go around creating an array for the image file names as i am dealing with more than 5k images , maybe i can use a for loop to achieve this ... i have been at it straight for couple of hours so i am frustrated and can't think logically so would appreciate some help ...
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,195
129
Attractor Field Beta
I don't know html or css.. but it seems you are having problem storing images in order to call them. You can't use array as you say there are 5k images. One solution could be to write names in a text file and read from text file whenever you want to load the image?
 

qaiser

M.Khubaib Qaiser
Mar 17, 2009
1,786
0
41
33
Islamabad
www.google.com.pk
You will have to make classes of all the images you need in CSS and then on hero selection remove all classes from the div (imageContainer) and apply new class depending upon the hero.
Something like this:

HTML:
<div id="imageContainer" class=""></div>​
CSS:
.img1{
background: url("(image path)/image1.jpg");
background-size: *define size here*;​
}
.img2{
background: url("(image path)/image2.jpg");
background-size: *define size here*;​
}​
and so on...
 

HolyCrap

Clueless.
Mar 26, 2013
5
0
1
Somewhere
You sure you don't need a backend for this? Loading a new image everytime a person selects a specific item seems like a bad idea, you could cache images, but we are talking 5k images here, that would make the process even slower.
 

mubaidr

Me Gusta
Jan 15, 2010
1,860
3
44
Rawalpindi
mubaidr.github.io
-Do you know the location where images are stored? If yes, this is how you load an image from the web and insert in to html content:

var img = new Image();
img.onload = function(){
document.getElementById('container').appendChild(this);
};
img.src= image_url_here;
 
Last edited:
General chit-chat
Help Users
IMPORTANT: Please Change Your Passwords to avoid being botted. | Click for Discord
  • No one is chatting at the moment.
  • Aciel Aciel:
    This is the error.
    Link
  • Aciel Aciel:
    Online session is enabled for UBL. I have an active subscription with my NayaPay card (it is already added to Google Payment Methods).
    Link
  • B Baghi:
    did you try setting up Google Payments? For UBL you may have to get it enabled for internet transactions, Naya should work without it!
    Link
  • Aciel Aciel:
    I tried both NayaPay, and UBL, but I keep getting "correct country selected..." error. I already have PK set there 🤦‍♂️
    Link
  • Aciel Aciel:
    Baghi said:
    Link
  • B Baghi:
    Yes
    Link
  • Aciel Aciel:
    Does your VISA Cards work on Google Play Store?
    Link
  • M murtaza12:
    XPremiuM said:
    Why? Was the site hacked or something?
    Yes
    Link
  • XPremiuM XPremiuM:
    GloriousChicken said:
    Everyone, please change your passwords.
    Why? Was the site hacked or something?
    Link
  • GloriousChicken GloriousChicken:
    Everyone, please change your passwords.
    Link
  • Necrokiller Necrokiller:
    The only valid thing from his pov he said in the video is AC dead since Black Flag. According to woke police that game was woke too. Welsh man in West Indies. So atleast he's consistent I guess lol
    Link
  • Link
  • XPremiuM XPremiuM:
    Necrokiller said:
    It's based on an actual real life person so I don't think the woke police have a valid case here.
    Nope. They have a very valid case. The above video explains it all.
    Link
  • Necrokiller Necrokiller:
    It's based on an actual real life person so I don't think the woke police have a valid case here.
    • Like
    Reactions: SolitarySoldier
    Link
  • XPremiuM XPremiuM:
    Meanwhile Ghost of Tsushima PC version is out now. Looks 100 times better than ASS Creed already.
    Link
  • XPremiuM XPremiuM:
    Did y'all see the new Assassin's Creed trailer? They finally made a AC set in Japan & then they put a negro as the male protagonist. Ubisoft is taking cues from Disney, and it isn't gonna end well for them, just like Disney. Go woke, go broke!
    Link
  • Necrokiller Necrokiller:
    First Fallout 4 update and now this 🤡
    Link
  • Necrokiller Necrokiller:
    MS and Bethesda continuing their streak of massive Ls 😬
    Link
  • Link
  • funky funky:
    Hello
    Link
  • NaNoW NaNoW:
    by closing down good studios
    Link
  • NaNoW NaNoW:
    well he is breaking barriers
    • Like
    Reactions: KetchupBiryani
    Link
  • iampasha iampasha:
    SolitarySoldier said:
    Phil keeps talking about breaking barriers to gaming, making it accessible on all platforms yada yada, while killing competition and creativity at the same time. the fact that i actually believed him for a second lol
    guys the biggest yapper in the Industry right now. All he do is yap
    Link
  • Necrokiller Necrokiller:
    Phil should be held responsible for this shitfest too, just like Sarah, but it's highly likely that these decisions are coming from Satya. And this isn't even the end of it. More closures are coming.
    Link
  • SolitarySoldier SolitarySoldier:
    if we are moving towards more and more popular trash across platforms that make billions for companies, I'm happy with all the barriers and exclusivity because at least that brings some pressure to create good stuff.
    Link
    Aciel Aciel: This is the error.