Ask coding questions

← Back to all posts
Random items with HTML
jort57

Hi! I'm a (very) beginner at HTML, CSS, and JS. I have pretty much no knowledge of using JS with HTML other than using alert(). And I was thinking of making a little 'splash text' (Go look, at Minecrafts) that would be like a subtitle. I was thinking there would be a array of splash texts, where it would then pick a random item from that array and display it on an h3 text in HTML. If that's possible, could someone please show me how that could work?

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
jort57
Comments
hotnewtop
Coder100

Sure, so your html looks like:

And your js:

Wow... There's a lot of things going on. Make sure you understand all of them! Here are some links:

jort57

@Coder100 I tried using the code but it says:

TypeError: Cannot set property 'innerHTML' of null
at /script.js:3:50
TypeError: Cannot set property 'innerHTML' of null
at /:13:53

Not surewhat this means. Can you help me?

Coder100

send repl @jort57

Coder100

you most likely forgot to add the html @jort57

jort57
Coder100

why did you put the script.js before the HTML code? It won't be able to access those elements silly @jort57

Coder100

correct html:

@jort57

jort57

@Coder100 Thanks!

Coder100

np! @jort57

RYANTADIPARTHI

Sure, use the Math.random() in your javascript.

SixBeeps

Nah man, random is impossible.

Just kidding lol. Take a look at the random() function, which returns a random number between 0 and 1. If you multiply it by 3 then floor it, you'll get a number between 0 and 2 inclusive.

jort57

@SixBeeps could you give me some code if ossible? (and also include the line where it puts it as an h3 HTML line if you can)

SixBeeps

@jort57 Something like this?