This is an old revision of the document!
We'll build on the work you've done so far so if you need to, copy the code below into jsfiddle.
<h1 style="font-family:arial, sans-serif; color: green; font-size: 1.7em; text-decoration: underline; font-variant:small-caps; text-shadow:2px 2px #ff0000;"> My Story </h1> <p style="text-align:right"> My friend Pablo has a <em style="color:red; font-weight:700">pet dog</em> called Sam who likes to have his tummy scratched. When they go to the park, <strong>he likes to chase a <em>ball</em>.</strong> He rolls in the dirt so Pablo has to wash him when they get home. </p> <p style="text-indent:50px"> On Sundays they drive to the <sup>mountains</sup> where he runs around smelling the grass and <span style="color:blue;">wagging his tail</span>. He gets so tired that he sleeps all the way home. </p>
We're going to turn this story into a game. You may have played this as a child. The user will be asked to provide words fitting certain categories, which will be placed into the story to make something ridiculous. Take a look at the final result here → https://jsfiddle.net/maganthro/cen6tj0L/12/.
// make background black to hide existing text // collect input from user // show input in story // make background white to show text again
//
denotes a comment in Javascript. The browser will ignore everything after the //
until the end of the line.<body style="background-color:black";>
document.body.style.backgroundColor = "black";
.
is often used to denote a sub-element of the parent object. Javascript uses this convention so 'document.body' refers to the <body>
tag (and everything within it) of the html code..style
works on every element to change something in the element's style attribute.document.body.style.backgroundColor
.-
. Javascript runs all the words together but capitalises the first letter after a join.="black"
in the same way in algebra we assign a value to a variable like x = 6
"
to tell Javascript that it's text.;
. This tells the browser that the command is complete and the next text it finds will be a separate command.animal = prompt("Give me an animal");
animal
.prompt()
to pop up a box with a message and an input box for the user.animal = prompt("Give me an animal"); name = prompt("Give me a name"); bodypart = prompt("Give me a body part"); noun = prompt("Give me a noun"); place = prompt("Give me a place"); action = prompt("Give me an action ending in 'ing'");
document.getElementsByTagName('span')[1].innerHTML = noun;
document
to indicate we're referring to the HTML code (rather than a variable or object we've created).document.getElementsByTag()
finds the list of elements with the specified tag in the order they appear in the HTML code.<em>
tag.<em>
, so why have we written 1
? Like many (all?) computer programs, Javascript starts counting at 0
, so the first occurrence is 0 and the second is 1.<em>
tag, so we use .innerHTML
to select that. = noun
Notice that noun
isn't wrapped in inverted commas because noun
isn't text itself. It is the name of a variable that represents text.<p style="text-align:right"> My friend Pablo has a <em style="color:red; font-weight:700">pet dog</em> called <span>Sam</span> who likes to have his <span>tummy</span> scratched. When they go to the park, <strong>he likes to chase a <em>ball</em>.</strong> He rolls in the dirt so Pablo has to wash him when they get home. </p> <p style="text-indent:50px"> On Sundays they drive to the <sup>mountains</sup> where he runs around <span>smelling</span> the grass and <span style="color:blue;">wagging his tail</span>. He gets so tired that he sleeps all the way home. </p>
document.getElementsByTagName('span')[0].innerHTML = animal; document.getElementsByTagName('span')[1].innerHTML = name; document.getElementsByTagName('span')[2].innerHTML = bodypart; document.getElementsByTagName('span')[4].innerHTML = action; document.getElementsByTagName('em')[1].innerHTML = noun; document.getElementsByTagName('sup')[0].innerHTML = place;
// make background black to hide existing text document.body.style.backgroundColor = "black"; // collect input from user animal = prompt("Give me an animal"); name = prompt("Give me a name"); bodypart = prompt("Give me a body part"); noun = prompt("Give me a noun"); place = prompt("Give me a place"); action = prompt("Give me an action ending in 'ing'"); // show input in story document.getElementsByTagName('span')[0].innerHTML = animal; document.getElementsByTagName('span')[1].innerHTML = name; document.getElementsByTagName('span')[2].innerHTML = bodypart; document.getElementsByTagName('span')[4].innerHTML = action; document.getElementsByTagName('em')[1].innerHTML = noun; document.getElementsByTagName('sup')[0].innerHTML = place; // make background white to show text again document.body.style.backgroundColor = "white";