0

I realise this and similar questions / answers exist already. But I've looked at a lot now and just not sure what simple thing I'm doing wrong. I'm just getting to grips with this. I have the error "Can't find variable: getElementById" I've try changing the order of the script and HTML. I used this random method which works fine Get random item from JavaScript array Its just applying it to the src...

This video was also helpful https://www.youtube.com/watch?v=pqLS4oyJ8cA

Here is my code: http://jsfiddle.net/udkhpytm/

<div>
<script id="IntroAnimation" type="text/javascript" charset="utf-8" src=""></script>
</div>

<script type="text/javascript">
var my_array = ['FILE1', 'FILE2', 'FILE3'];
var ri = Math.floor(Math.random() * my_array.length); // Random Index position in the array
getElementById("IntroAnimation").src = ri;
</script>

I'm trying to get a random item from the array and place it in the src of my Script by the scripts ID.

Community
  • 1
  • 1
pinkp
  • 445
  • 2
  • 12
  • 30
  • My final working code::-
    – pinkp Aug 19 '14 at 16:43

2 Answers2

5

getElementById is a method belonging to the document object. It isn't a standalone function. Change:

getElementById("IntroAnimation").src = my_array[ri];

To:

document.getElementById("IntroAnimation").src = my_array[ri];

Note that I've also changed ri to my_array[ri] as otherwise you're passing in the index position as the src rather than the contents of the array at that specific position.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • Both work great thanks to you both. However its only working on jsfiddle but not locally.. There is no error. It just doesn't apply it to the src.. any ideas? thanks – pinkp Aug 19 '14 at 14:17
  • 1
    @pinkp see this answer here for how to dynamically load JavaScript: http://stackoverflow.com/a/14521482/1317805 – James Donnelly Aug 19 '14 at 14:19
  • I tried my best with that but don't really understand it entirely.. Its just gives me new errors... any chance you can help amend my html? [link]http://we.tl/7yasEthkqg, so its because its inside a Script tag? As it works with img. – pinkp Aug 19 '14 at 14:42
  • 1
    @pinkp When you change the `src` of an image you need to check to see whether it has loaded properly, otherwise the change will not be smooth and may fail. The reason you want to load the new script using JavaScript rather than modifying HTML is for this same reason. You need to check to see that it's actually worked. – James Donnelly Aug 19 '14 at 14:48
  • OK I see, so how do apply your link http://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js/14521482#14521482 to my script.. I have tried and failed. thank you – pinkp Aug 19 '14 at 14:54
  • 1
    @pinkp the `script.src = something` part would be changed to `script.src = my_array[ri]`. – James Donnelly Aug 19 '14 at 14:54
1

You need to grab that item from the array, all you did was just get a random number based on the array length.

var my_array = ['FILE1', 'FILE2', 'FILE3'];
var ri = Math.floor(Math.random() * my_array.length);
var file = my_array[ri];

Then you need to add document before getElementById:

doucment.getElementById("IntroAnimation").src = file;