Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to add randomly generated text

Hi,

So I am playing around with a website based off of a tutorial with Jquery I've followed on treehouse.

There is a "spoiler alert" button codes like this...

$(".spoiler").append("<button>click here!</button>");

I am trying to make the text that appears on the button randomly generate to different text from an array each time the page is refreshed. I have found this code to be relevant but I am not sure how to implement.

<script language="JavaScript"> <!-- var r_text = new Array (); r_text[0] = "All the leaves are brown"; r_text[1] = "And the sky is grey"; r_text[2] = "I've been for a walk"; r_text[3] = "On a winter's day"; r_text[4] = "I'd be safe and warm"; r_text[5] = "If I was in L.A."; r_text[6] = "California dreaming, On such a winter's day"; var i = Math.floor(7*Math.random())

document.write(r_text[i]); </script>

Thank you for the help!

Anthony

1 Answer

Hi,

i hope i understand your question right... so here is a simple example of what you are trying to do.

example link

Hope this helps!