#5 td .red
["un", "deux", "trois", "quartre", "cinq"]Use an anonymous functions in arrow notation for the event handlers.
<!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.
document.write(Boolean(5) + " "); document.write(Boolean(283764) + " "); document.write(Boolean(0) + " "); document.write(Boolean(-203) + "<br>"); // Output: true true false true document.write(Boolean(3.14159265) + " "); document.write(Boolean(-284.378) + " "); document.write(Boolean(0.000000001) + " "); document.write(Boolean(0.0) + "<br>"); // Output: true true true false document.write(Boolean("grapefruit") + " "); document.write(Boolean("$%&*@") + " "); document.write(Boolean("0") + " "); document.write(Boolean("false") + " "); document.write(Boolean(" ") + " "); document.write(Boolean("") + "<br>"); // Output: true true true true false document.write(Boolean(null) + " "); document.write(Boolean(undefined) + " "); document.write(Boolean(Infinity) + " "); document.write(Boolean(NaN) + "<br>"); // Output: false false true falseThen run it to check if your prediction is correct.
var n = 45; var v; if (n) { v = "nonzero"; } else { v = "zero"; } document.writeln("Value: " + v);
<head> <script src="../jquery-3.5.1.js"> </script> ... other script tags. </head>Place the jQuery library script in the parent folder of your submission file so it is not included in your submission file. I will supply my own copy of this file in the parent folder when I grade your submission.
<head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> ... other script tags. </head>
$(selector).action( );
// Hide all elements with node name p. $("p").hide( ); // Change the value of the input // controls with class="test" to "abc". $(".test").val("abc"); // Change the inner html of the paragraph // with id="p1" to "Hello, World!". $("#p1").text("Hello, World!");
window.addEventListener("load", init);is
$(document).ready(init);or even shorter
$(init);
<!DOCTYPE html> <html lang="en"> <head> <script src="../jquery-3.5.1.js"> </script> <script src="myscript.js"> </script> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>jQuery Examples -- Set 1</h1> <button id="btn1">Show First Greeting</button> <input type="button" id="btn2" value="Show Second Greeting"> <p id="p1"></p> </body> </html>Translate these JavaScript scripts into jQuery. In each case, place the script in myscript.js to run the example.
// JS1: myscript.js // display the greeting "Hello, World!" in // the paragraph p1 when the window loads. function init( ) { document.getElementById("p1").innerHTML = "Hello, World!"; } window.addEventHandler("load", init);The init method could be passed to window.addEventHandler as an anonymous function like this:
// JS2: myscript.js window.addEventHandler("load", function( ) { document.getElementById("p1").innerHTML = "Hello, World!"; });Here is the JavaScript Script JS1 translated into jQuery (JQ1), using $ as an abbreviation for $(document).ready:
$(function( ) { $("#p1").text("Hello, World!"); });
// JQ1: myscript.js $(function( ) { $("btn1").click(function) { $("#p").text("Hello, World!"); }); });
jQuery | JavaScript |
---|---|
$("#p1") | document.querySelector("#p1"); document.getElementById("p1"); |
$(".red") | document.querySelectorAll(".red"); document.getElementsByClassName("red"); |
$("p") | document.querySelectorAll("p"); document.getElementsByTagName("p"); |