// a.
$(document).ready(init);
// b.
$(init);
// c.
$("button").click(f);
// d. p1 is the id of a paragraph
$("#p1").text("This is a test.");
// e.
var paraContent = $("#p1").text( );
// f.
$("input").val("Test input");
// g.
var inputContent = $("input").val( );
// h.
$("button").click( ( ) => $("button").hide( ));
$(document).ready(init);is usually written as its shorter version
$(init);Rewrite this script as its shorter version and test it:
$(document).ready( ( ) => {
$("button").click( ( ) => {
$("p").text("Hello, World!");
});
});
Halloween Greeting Example
Happy Halloween
$( ( ) => {
$("#p1").html("Halloween Greeting Example<br>Happy Halloween");
$("#p1").css("font", "bold 200% Chiller");
$("#p1").css("backgroundColor", "#FF8000");
});
Translate this script to Vanilla JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise 2</title>
<style>
#img1, #img2, #img3 {
display: inline;
width: 15px; height: 15px;
background-color: #C0C0C0; }
</style>
<script>
function showIcon(e) {
var cbObj = e.target;
var cbIndex = cbObj.id.charAt(2);
var imgObj = document.querySelector("#img" + cbIndex);
var imageFile = cbObj.checked ?
"green-check.png" : "red-x.png";
imgObj.src = imageFile;
}
function init( ) {
for(var i = 1; i <= 3; i++) {
document.querySelector("#cb" + i).
addEventListener("change", showIcon);
}
}
window.addEventListener("load", init);
</script>
</head>
<body>
<h1>Exercise 2</h1>
<ol>
<li><img id="img1">
<input type="checkbox" id="cb1">
<label for="cb1">Checkbox 1</label></li>
<li><img id="img2">
<input type="checkbox" id="cb2">
<label for="cb2">Checkbox 2</label></li>
<li><img id="img3">
<input type="checkbox" id="cb3">
<label for="cb3">Checkbox 3</label></li>
</ol>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>MathRiddles1 Example</title>
</head>
<body>
<ol>
<li><span id="r1">
Why is 6 scared of 7?</span><br>
<span id="a1">
Because 7 8 9.</span>
<button>Show Answer for Riddle 1</li>
<li><span id="r2">
How can you tell that the three fractions
x/c, y/c, and z/c all live in a foreign country?</span><br>
<span id="a2">
Because they are all over cs.</span>
<button>Show Answer for Riddle 2</li>
<li><span id="r3">
What did the repeating decimal number say to pi?</span><br>
<span id="a3">
Don't be so irrational.</span>
<button>Show Answer for Riddle 3</li>
</ol>
</body>
</html>
The each answer is hidden, but can be displayed when the button for
its riddle is clicked.
// Example 1
// Use 1 paragraph in HTML file with id="p1".
$(function( ) {
$("#p1").mouseleave(function( ) {
alert("You left paragraph #p1.");
});
});
// Example 2
// Use 4 paragraphs in HTML file.
$(function( ) {
$("p").mouseleave(function( ) {
var t = $(this).text( );
alert(`Bye! Left the <p> with text ${t}.`);
});
});
// Example 3
// Use 3 input elements with type="text".
$(function( ) {
$("input").focus(function( ) {
$(this).css("background-color", "#cccccc");
});
$("input").blur(function( ){
$(this).css("background-color", "#333333");
});
});
// Example 4
// Use 5 paragraphs in HTML file.
$(function( ) {
$("p").on("click", function( ) {
$(this).hide( );
});
});
// Example 5
// Use buttons with captions "Hide" and "Show",
// and also a paragraph with no ID.
$(function( ) {
$("#hide").click(function( ) {
$("p").hide( );
});
$("#show").click(function( ){
$("p").show();
});
});
See the W3Schools site for the complete HTML files, which can be run interactively. Here are
the HTML files with scripts (jQuery and
Vanilla JS) that we looked at in class.