nickycdk
nickycdk

Reputation: 53

GetElementById is not a function?

When running this script i get an error message that document.GetElementById is not a function?? - You can see the site at: http://www.fritidsjobbere.dk:

//Show random div with Javascript
antal = 6;
var randomnumber=Math.floor(Math.random()*antal);
if(randomnumber == "0") {
randomnumber = 1;
}
//alert(randomnumber);
document.getElementsById("partner-ad"+randomnumber).style.display = 'block'; 

Upvotes: 2

Views: 5816

Answers (4)

Nick Craver
Nick Craver

Reputation: 630587

It's singular (IDs are unique, it should only return one result), document.getElementById like this:

document.getElementById("partner-ad"+randomnumber).style.display = 'block'; 

Also, your actual code on the site differs from your question, it's document.getElementsByID on the actual page...make sure that ID is Id as well.


Another side-note, since your ads are partner-ad1 through partner-ad6, your current code would never show the 6th ad, you can simplify it and fix that issue like this:

var antal = 6;
var randomnumber=Math.ceil(Math.random()*antal); //instead of Math.floor
document.getElementById("partner-ad"+randomnumber+"").style.display = 'block';

Upvotes: 2

Mutation Person
Mutation Person

Reputation: 30520

Are you confusing getElementById and getElementsByTagName. Note the former is singular, and the latter is plural.

Of course, if you're using jQuery (as your tag intimates), you needn't be using either, as you can just use the #ID selector:

$("#partner-ad"+randomnumber").show();

Upvotes: 2

Rob
Rob

Reputation: 6871

document.getElementsById("partner-ad"+randomnumber).style.display = 'block';

Should be

document.getElementById("partner-ad"+randomnumber).style.display = 'block';

Why not do this using jQuery by the way?

Upvotes: 1

Nikita Rybak
Nikita Rybak

Reputation: 68026

getElementsById -> getElementById

Upvotes: 14

Related Questions