0

So, I did not think that this would that complicated but I cannot figure it out myself.

I preload images in the <head> with JS.

//PRELOAD THE IMAGES

original = new Image(698, 360);
original.src = "imagenes_png/bosque_mapa.png";

...

Then I want to insert the preloaded images ito a div tag with the name test, also with JS.

//Thats in the head aswell
document.getElementById('test').appendChild(original)

That is my div tag in the body

<div id="test"> testext</div>

That does not work though. The div is still testext


For people with the same issue:

Put the

document.getElementById('test').appendChild(original)

into here

$(document).ready(function() { /* code here */ });

$(document).ready(function() {document.getElementById('test').appendChild(original)});

Why? Read the comment of David Thomas and follow the posted link!

**Note: That solution requieres jQuery which can be loaded like this:

<!-- LINK ZU JQUERY ONLINE-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Maccaroni
  • 19
  • 7
  • 2
    If you're appending, or trying to append, in the `` it's very likely that the elements you're trying to append *to* won't yet exist, see: http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – David Thomas Oct 17 '14 at 17:19
  • @DavidThomas ah thanks a lot. I was looking for mistakes in my code or something but that makes sense! – Maccaroni Oct 17 '14 at 17:21
  • The easiest way to fix this is to put the script at the end of your `body` element – Brennan Oct 17 '14 at 17:22
  • @brennan I am working with Joomla so might have to add a little peace of javascript instead of putting the script at the end. But good to know that its not my syntax. I was freaking out why it did not show anything :) – Maccaroni Oct 17 '14 at 17:23

0 Answers0