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,787
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
We have disabled traderscore and are working on a fix. There was a bug with the plugin | Click for Discord
  • No one is chatting at the moment.
  • NaNoW NaNoW:
    by closing down good studios
    Link
  • NaNoW NaNoW:
    well he is breaking barriers
    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
  • SolitarySoldier SolitarySoldier:
    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
    Link
  • SolitarySoldier SolitarySoldier:
    "These changes are not a reflection of the creativity and skill of the talented individuals at these teams or the risks they took to try new things" ... seems to me that's exactly what it is
    Link
  • SolitarySoldier SolitarySoldier:
    why make good games when u can just buy everyone and shut them down lol
    Link
  • XPremiuM XPremiuM:
    I'm gonna say one last time, F*** Microsoft to infinity!
    Link
  • XPremiuM XPremiuM:
    Microsoft deserves all the hate they can get. Seriously i can't explain how much i want to curse them out.
    Link
  • XPremiuM XPremiuM:
    They could've sold the studios instead of closing them, but the nazi bastards just didn't want competition down the road.
    Link
  • Link
  • XPremiuM XPremiuM:
    F*** Microsoft, and F*** their fanboys.
    Link
  • XPremiuM XPremiuM:
    What's the f*** is wrong with them? I mean really? Have they completely lost it? F***ing retards.
    Link
  • XPremiuM XPremiuM:
    So i just found out that f***ing s***bag Microsoft shut down Arkane Studio (makers of the brilliant Dishonored series) and Tango Gameworks (makers of the iconic The Evil Within series), among some other studios. I just want to say a giant F*** Y**! to Microsoft. THEY'VE F***ED UP BIG TIME this time.
    Link
  • Necrokiller Necrokiller:
    MS: Hold my trillion dollars
    Link
  • Necrokiller Necrokiller:
    Sony: We can f**k up a totally good situation.
    Link
  • Link
  • XPremiuM XPremiuM:
    Started Dead Island 2, and i find it pretty lame. The story is shit. The characters are either cringe or bland (all 6 of them), The gameplay isn't fun. Even the goddamn zombies don't behave like proper zombies. They're all fast & intelligent unlike how zombies are supposed to be like. Any fan of "zombie" genre cannot possibly like this shitfest of a game. Dying Light 2 was million times better than this shit.
    Link
  • NaNoW NaNoW:
    so Baldurs Gate 3 is pretty great!"
    Link
  • faraany3k faraany3k:
    So who here is waiting for Senua Hellblade 2. Prequel was a true mind fuck experience.
    Link
  • GloriousChicken GloriousChicken:
    Sad indeed
    Link
  • NaNoW NaNoW:
    what a sad day
    Link
  • faraany3k faraany3k:
    Necrokiller said:
    MS one upping Sony in catching the biggest L in gaming
    tbh gaming industry as a whole is failing much like tech industry. They have buckled down too much to Corporate suites then relying on passionate and innovative people. A corporate only believes in financial growth and shareholders.
    Link
  • Necrokiller Necrokiller:
    MS one upping Sony in catching the biggest L in gaming
    Link
    NaNoW NaNoW: by closing down good studios