Table of Contents

Web Development Lesson 2 - Text

Javascript

Objectives

In this exercise you'll get your first taste of Javascript and use it to create a simple game, where you get a user to change key words in your story to make it funny.

Setup

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>

Preview

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/.

Comments

// make background black to hide existing text

// collect input from user

// show input in story

// make background white to show text again

Object Notation

document.body.style.backgroundColor = "black";

Prompt

animal = prompt("Give me an animal");
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'");

Editing the document text

document.getElementsByTagName('em')[1].innerHTML = noun;
<p style="text-align:right">
My friend Pablo has a <em style="color:red; font-weight:700">pet <span>dog</span></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;

Final Code

// 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";

Next: Exercise