<!-- Stan Smith Project 3 Feb 6, 2021 --> <!DOCTYPE html> <html lang="en"> ....Answer: The first line of the HTML page must be
<!DOCTYPE html>Source code comments or other code should go after that. Here is what the beginning of the HTML file should look like:
<!DOCTYPE html> <!-- Stan Smith Project 3 Feb 6, 2021 --> <html lang="en">
<input type="button" id="btn" value="Click Me">You can also use a button tag:
<button id="btn">Click Me</button>Then use this script.js file:
function changeColor( ) { var bodyObject = document.getElementById("b"); bodyObject.style.backgroundColor = "red"; } function init( ) { document.getElementById("btn"). addEventListener("click", changeColor); } window.addEventListener("load", init);The preceding lines can be written using anonymous methods for init and changeColor:
window.addEventListener("load", ( ) => { var bodyObject = document.getElementById("btn"); bodyObject.addEventListener("click", ( ) => { document.getElementById("btn") .bodyObject.style.backgroundColor = "red"; }); });
<!DOCTYPE html> <!-- Test Coin Flip Example: Source code file: test-coin-flip.html --> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test Coin Flip</title> <style> img { width: 250px; } </style> <script> function flipCoin( ) { var image1 = document.getElementById("coin"); var randomValue = Math.random( ); if (randomValue >= 0.5) { image1.src = "heads.png"; } else { image1.src = "tails.png"; } } window.addEventListener("load", flipCoin); </script> </head> <body> <h1>Test Coin Flip</h1> <img id="coin" alt="Heads or Tails"> </body> </html>
var a = [34, 72, 19, 5, 90, 7];
var s = "abc"; var t = new String("abc");To see this, run these JavaScript statements in a script:
var s = "abc"; var t = new String("abc"); document.writeln((typeof s) + " " + (typeof t)); // Output: string object
class Kid { // Constructor for initializing // Kid properties constructor(name, gender, age) { this.name = name; this.gender = gender; this.age = age; } // Define instance methods haveBirthday( ) { this.age++; } toString( ) { return `${this.name};${this.gender};${this.age}`; } } // Test Kid class by creating a Kid object. k = new Kid("Alice", "F", 11); document.writeln(`${k.name} ${k.gender} ${k.age}<br>`); document.writeln(`${k.toString( )} ${k}<br>`); k.haveBirthday( ); document.writeln(k.age);
class Clock { constructor(hr, min, sec) { this.hr = hr; this.min = min; this.sec = sec; } tick( ) { this.sec++; if(this.sec == 60) { this.sec = 0; this.min++; } if(this.min == 60) { this.min = 0; this.hr++; } if(this.hr == 24) { this.hr = 0; } } toString( ) { var h = this.hr.toString( ).padStart(2, "0"); var m = this.min.toString( ).padStart(2, "0"); var s = this.sec.toString( ).padStart(2, "0"); return `${h}:${m}:${s}`; } } c = new Clock(20, 44, 9); document.writeln(c + "<br>"); for(let i = 1; i <= 20000; i++) { c.tick( ); } document.writeln(c);
var lottoObject = new Lotto(30, 5); lottoObject.selectBalls( ); document.writeln(lottoObject); for(let i = 1; i <= 5; i++) { document.writeln(lottoObject.getBall(i)); }
var count = 0;
// for loop 1 function testScope( ) { for(var i = 1; i <= 5; i++) { } return i; } document.writeln(testScope( )); // Output of for loop 1 6 // for loop 2 function testScope( ) { for(let i = 1; i <= 5; i++) { } return i; } document.writeln(testScope( )); // Output of for loop 2 No output. The i in the return statement is not valid because it is outside of the loop where it is defined.
<input> <button> <textarea> <select> <option>HTML Tags Listed Alphabetically (W3Schools)
id class hidden style disabled dragableHTML Global Attributes (W3Schools)
<input> type (button checkbox radio hidden number password color date email file image month range reset search submit tel text time url week) value (text) checked (checked) max (number) maxlength (number) min (number> minlength (number) multiple (multiple) pattern (regexp) placeholder (text) readonly (readonly) required size (number) step (number) <button> <textarea> cols (number) rows (number) readonly (readonly) maxlength (number) placeholder (text) wrap (hard soft) <select> multiple (multiple) size (number) <option> value (text) selected (selected)Tag Attibutes Listed Alphabetically (W3Schools)
blur change click dblclick focus input keydown keypress keyup load mousedown mouseenter mouseleave mousemove mouseup resize scroll select submit wheel
HTML Events (W3Schools)