Spunky Posted June 8, 2011 Share Posted June 8, 2011 HTML code: <p id="replace">Hello</p> Javascript code: var randomNumber;var previouslyUsedRandomNumbers = "";var r; for(var r = 1; r <= 1; r++){ do { randomNumber = Math.floor(Math.random() * images.length); alert("Length: " + images.length); } while(previouslyUsedRandomNumbers.indexOf("|" + randomNumber + "|") >= 0); alert(randomNumber); alert(images[randomNumber].valueOf()); previouslyUsedRandomNumbers += "|" + randomNumber + "|"; alert(previouslyUsedRandomNumbers); document.getElementById("replace").innerHTML=images[randomNumber].valueOf(); } All of the alerts are there just to show me whats working. They are all working fine.when I run the code, it goes through alerts and then nothing happens. In the error log I get:document.getElementById("replace") is nullWhat I am confused about is why is it null. I even tried putting a different id in there from another 'p' tag that has been used by other document.getElementById code just fine. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 8, 2011 Share Posted June 8, 2011 Where is this code placed in relation to the rest of your code? Ie, is it in the head? If it's in the head, is it in a window.onload handler? If it's not in the head and it's in the body, is it before or after the element(s) in question?I suspect you are trying to access the element before the DOM has loaded. Link to comment Share on other sites More sharing options...
Spunky Posted June 8, 2011 Author Share Posted June 8, 2011 Where is this code placed in relation to the rest of your code? Ie, is it in the head? If it's in the head, is it in a window.onload handler? If it's not in the head and it's in the body, is it before or after the element(s) in question?I suspect you are trying to access the element before the DOM has loaded.Im linking to it using:<script type="text/javascript" src="random.js"></script>in the header so the code is in random.jsBut that is also where I have the other code that is using document.getElementById code from the same HTML file and it works fine. that javascript is just in a different file, but linked to in the same manner. Link to comment Share on other sites More sharing options...
ShadowMage Posted June 8, 2011 Share Posted June 8, 2011 Im linking to it using:<script type="text/javascript" src="random.js"></script>in the header so the code is in random.jsBut that is also where I have the other code that is using document.getElementById code from the same HTML file and it works fine. that javascript is just in a different file, but linked to in the same manner.Is your working code inside a handler for window.onload?Something like:window.onload = function() { //working code} or function init = function() { //working code}window.onload = init; Bottom line is if you don't have the code in an onload handler (or delay it long enough for the page to load) the script will try to access an element that doesn't exist. Hence why it returns null. You need to have the code in an onload handler, similar to the code above. Link to comment Share on other sites More sharing options...
Spunky Posted June 8, 2011 Author Share Posted June 8, 2011 Is your working code inside a handler for window.onload?Something like:window.onload = function() { //working code} or function init = function() { //working code}window.onload = init; Bottom line is if you don't have the code in an onload handler (or delay it long enough for the page to load) the script will try to access an element that doesn't exist. Hence why it returns null. You need to have the code in an onload handler, similar to the code above. Ok I get it. The other javascript, although it is also placed in the header, its in a function, that the HTML is calling when a button is clicked. This I have it just loading when the page loads. So you're right, that must be it. Thank you. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.