var result = condition ? resultIfTrue : resultIfFalse;This is just a one-line if-else statement:
var result; if (condition) { result = resultIfTrue; } else { result = resultIfFalse; }For example:
var x = 259; var size = (x >= 100) ? "Large" : "Small";
<!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 12</label></li> <li><img id="img3"> <input type="checkbox" id="cb3"> <label for="cb3">Checkbox 3</label></li> </ol> </body> </html>
$(document).ready(init);is usually written as its shorter version
$(init);Rewrite this script as its shorter version and test it:
$(document).ready( ( ) => { $("#btn1").click( ( ) => { $("#p1").text("Hello, World!"); }); });Answer: here is the entire HTML file with the revised script.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Exercise 3</title> <script> $( ( ) => { $("#btn1").click( ( ) => { $("#p1").text("Hello, World!"); }); }); </script> </head> <body> <h1>Exercise 3</h1> <button id="btn1">Show Greeting</button> <p id="p1"></p> </body> </html>
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. Answer:
window.addEventListener("load", ( ) => { var pObj = document.querySelector("#p1"); pObj.innerHTML = "Halloween Greeting " + "Example<br>Happy Halloween"; pObj.style.font = "bold 200% Chiller"; pObj.style.backgroundColor = "#FF8000"; });
<!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.