Here are some mini exercises to challenge your jQuery skills a bit.
Create a new HTML page locally with jQuery installed in it, called jquery-hw.html
HTML
<button id="clicker">Click me to show a hidden secret!</button>
<div id="secret" style="display: none">I am a hidden secret.</div>
Instructions:
- Add jQuery code that fades in the text "I am a hidden secret" whenever the button is clicked.
- Change your jQuery code so it slides down the text instead of fading it in.
- Change the button text to say "Click me to toggle a hidden secret!" and make the text toggle between fading in and out each time it is clicked.
HTML
<span>Enter your name:</span>
<input type="text" id="name">
<button id="greet">Greet me!</button>
JavaScript
// Call say("Hi!") to have your computer say hi!
// This only works on recent versions of Safari
// and Chrome at the moment.
function say(text) {
var msg = new SpeechSynthesisUtterance(text);
window.speechSynthesis.speak(msg);
}
Instructions:
Add jQuery that greets the user by calling the
above say
function whenever the user clicks
the "Greet me!" button, taking into account the value of the text field.
For example, if the user types the word "Bob" into the text field, then the computer should say "Hello Bob!" when the button is clicked.
Hint: Google "getting text input jquery"
HTML
<button id="clicker">Click me to hide the hidden secrets!</button>
<p class="secret">I am hidden secret #1.</p>
<p class="secret">I am hidden secret #2.</p>
<p class="secret">I am hidden secret #2.</p>
<p>I am <em>not</em> a hidden secret.</p>
Instructions:
Add jQuery code to modify the page so that when the user clicks the button, the paragraphs that start with the words "I am a hidden secret" slide up.
<img src="https://images.prismic.io/wham-site/634e2cbf-8c64-4f57-9df3-3552508d6412_220315_Whaam_Henry_Fey1649_small.jpg">
<p>
<button id="clicker">Click me to change the picture</button>
</p>
Instructions:
Change the page with jQuery so that when the button is clicked, the image changes to this:
Hint: Change the src
attribute of the image.