# Solved: Generating Random Words (JavaScript)



## marques_uk (Jun 22, 2009)

Hello everyone,

I've found this JavaScript code which generates a random word everytime you click on the button in the xhtml page.

I've placed this in an external .js file.



This is applied to the XHTML page

This does exactly what I want but I would like to display the random word on the page rather then in a text field. Does anyone know how I can alter the code to do this?

I want it so the user can just keep clicking on the button to change the word as many times as they like.

Does anyone know how to make this happen?

Thanks in advance
Mark


----------



## andythepandy (Jul 2, 2006)

Hi Mark,
If you create a div(http://www.w3schools.com/tags/tag_div.asp) on your page and give it an id, for example:

```
<div id="ranWord">
</div>
```
You can then insert text into that with your javascript like:

```
document.getElementById('ranWord').innerHTML = 'text';
```
In your case you could have

```
---Change the PickRandomWord function to:
function PickRandomWord(output) {
// Generate a random number between 1 and NumberOfWords
var rnd = Math.ceil(Math.random() * NumberOfWords)

// Display the word inside the text box
document.getElementById(output).innerHTML = words[rnd]
}
---and your button:
<input type="button" onClick="PickRandomWord('ranWord');" />

---and the div:
<div id="ranWord">
</div>
```
Hope that works,
Andy


----------



## tomdkat (May 6, 2006)

Neat tip! 

Peace...


----------



## andythepandy (Jul 2, 2006)

:up: Cheers!


----------



## marques_uk (Jun 22, 2009)

thank you Andy, thats exactly what I was looking for. Works perfectly.

Kind regards
Mark


----------

