section idea/code by Stefanie Hatcher
This section is about using JavaScript's Document Object Model (DOM). The following links may be helpful:
(Click the image below to run the sample solution for today's section.) (solution JS code attack.js
)
A raptor is on the loose. Rawr! He wants to stomp the townspeople. Write JavaScript code to allow the raptor to eat them. The HTML and CSS are already completely written; start from this skeleton of attack.html.
Here are the behaviors to add:
div
with id
of people
as div
s with the class
of person
. Assign them the additional class boy
when the page loads (while retaining the class person
).
<div id="people"> <div class="person"></div> <!-- give these 5 divs the class 'boy' --> <div class="person"></div> <div class="person"></div> <div class="person"></div> <div class="person"></div> </div>
addClassName
function.
div
with the classes of person
and either boy
or girl
.
<button id="add">Add!</button>
splat
(in addition to their existing person
class, but in place of their gender class such as boy
or girl
).
<button id="kill">Kill!</button>
<label><input id="boys" type="radio" name="gender" /> Boys</label> <label><input id="girls" type="radio" name="gender" checked="checked" /> Girls</label>
div
s with class splat
).
<button id="cleanup">Clean up!</button>
remove
method.
img
tag with an id
of raptor
.
<button id="stomp">Stomp!</button>
top
style attribute to be either 10px
or 85px
.
getStyle
method.
enrage
to the raptor and the page's top h1
heading. In addition, the raptor should be made to be 50px wider than his current width. Clicking the button again removes the class from both elements and returns the width to its previous value. The h1
has an existing CSS class that should not be removed. You are guaranteed that there is exactly one h1
element on the page.
<button id="enrage">Enrage!</button>
left
position style is at least 300px
, he should change directions and start patrolling to the left until his left
position is 10px
or less, at which point he stops patrolling.
<button id="patrol">Patrol!</button>
setInterval
method.