29.07.2016 Views

front-end-developer_1_

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Front-End-Developer - Level 1<br />

$(document).ready(function() {<br />

$("button#hello").click(function() {<br />

$("ul").prep<strong>end</strong>("Hello!");<br />

});<br />

$("button#goodbye").click(function() {<br />

});<br />

$("ul").prep<strong>end</strong>("Goodbye!");<br />

$("button#stop").click(function() {<br />

$("ul").prep<strong>end</strong>("Stop copying me!");<br />

});<br />

});<br />

The .prep<strong>end</strong>() method will insert the text of the argument it's given into to the top of <br />

as the first child of the . (As you might guess, there's also a .app<strong>end</strong>() method that<br />

would insert at the bottom as the last child of the ; there are also .before() and<br />

.after() methods that would add the argument before or after the tag as siblings,<br />

rather than within it as children.)<br />

This is nice, but not a very fun conversation. Let's have the webpage say something different<br />

back to the user:<br />

scripts.js<br />

$(document).ready(function() {<br />

$("button#hello").click(function() {<br />

$("ul#user").prep<strong>end</strong>("Hello!");<br />

$("ul#webpage").prep<strong>end</strong>("Why hello there!");<br />

});<br />

$("button#goodbye").click(function() {<br />

$("ul#user").prep<strong>end</strong>("Goodbye!");<br />

$("ul#webpage").prep<strong>end</strong>("Goodbye, dear user!");<br />

});<br />

$("button#stop").click(function() {<br />

$("ul#user").prep<strong>end</strong>("Stop copying me!");<br />

$("ul#webpage").prep<strong>end</strong>("Pardon me. I meant no offense.");<br />

});<br />

});<br />

Of course, we need to change the tags to have IDs for user and webpage . For the<br />

sake of brevity, I won't bother showing the updated HTML here. Go ahead and do that<br />

yourself.<br />

DOM Manipulation and Traversal<br />

172

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!