var a = [ 129, 1123, 49, 9, 116]; a.sort( ); document.writeln(a);The output is
1123,129,116,49,9which is alphabetic order because the numbers are converted to strings. How can you get the Array sort method to sort the numbers in numeric order?
function compare(x, y) {
if (x >= y) {
return 1;
}
else if (x <= y) {
return -1
}
else {
return 0;
}
}
Then call the sort method like this:a.sort(compare);JavaScript programmers noticed that the compare method can be shortened to this:
function compareShort(x, y) {
return x - y;
}
This works because when
x >= y, x - y is positive, when
x <= y, x - y is negative, and
when x == y, x - y is zero.
These are the exact requirements of the compare function. We can pass this
compareShort function to the sort method like this:a.sort((x - y) => x - y);
click load dblclick focus blur keydown keyup mousedown mouseup mouseenter mouseleave
<!DOCTYPE html>
<!-- TestEvents Example; Source Code File: index.html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Test Events</title>
<script src="script.js"></script>
</head>
<body>
<h1>Test Events</h1>
<label for="txt1">Textfield 1</label><br>
<input type="text" id="txt1"><br><br>
<label for="txt2">Textfield 2</label><br>
<input type="text" id="txt2"><br><br>
<p id="p1">Results of Tests:</p>
</body>
</html>
---------------------------------------------------
// TestEvents Example
// File: script.js
// Write event listeners for
// focus, blur, keydown, and keyup events.
function recordFocus( ) {
var para1 = document.getElementById("p1");
para1.innerHTML += "<br>" +
"Focus event fired in Textfield 1";
}
function recordBlur( ) {
var para1 = document.getElementById("p1");
para1.innerHTML += "<br>" +
"Blur event fired in Textfield 1";
}
function recordKeyup( ) {
var para1 = document.getElementById("p1");
para1.innerHTML += "<br>" +
"Keyup event fired in Textfield 1";
}
function recordKeydown( ) {
var para1 = document.getElementById("p1");
para1.innerHTML += "<br>" +
"Keydown event fired in Textfield 1";
}
function init( ) {
var textField1 = document.getElementById("txt1");
textField1.addEventListener("focus", recordFocus);
textField1.addEventListener("blur", recordBlur);
textField1.addEventListener("keyup", recordKeyup);
textField1.addEventListener("keydown", recordKeydown);
}
window.addEventListener("load", init);
-------------------------------------------------------
h1 p td li span
<body>
<div id="content">
<h1>Sample HTML Page</h1>
<p class="info">Lorum ipsum dolor sit amet.</p>
<p>Lorum ipsum dolor <a href="#">sit</a> amet.</p>
</div>
<div id="nav">
<ul>
<li><a href="#" class="intro">Item 1<a></li>
<li><a href="#">Item 2</li>
<li><a href="#">Item 3</li>
</ul>
</div>
<div id="footer">
Lorum ipsum dolor <a href="#">sit</a> amet.
</div>
</body>
Here are some CSS selectors:
li { color: blue; }
.intro { font-weight: bold; }
p.intro { color: red; }
a.intro { color: green; }
#nav { color: blue; }
li a { color: green; }
#nav a { color: red; }
#nav ul li a { color: green; }
* { margin: 0px; padding: 0px; }
div > em { color: blue; }
h2 + h3 { margin: -1em; }
img[src="small.gif"] { border: 1px solid #000000; }
p::first-line { font-weight: bold; }
p::first-letter { font-size: 200%; font-weight: bold; }
A pseudo-element is a keyword that can be added to a
selector, to style a specific part of an element.
<body>
<h1>Planetary Outpost Status Summaries</h1>
<ol>
<li>Green Planet<br>
All is well.</li>
<li class="redtext">Red Planet<br>
All systems A-OK.</p>
</ol>
<h2>Number of Planets</h2>
<p id="para">3</p>
</body>
+--------- body ---------+
| / \ |
| / \ |
h1 ol h2 p
/ \
li li
body
h1
Planetary Outpost Status Summaries
ol
li
Green Planet<br>All is well.
li
Red Planet<br>All systems A-OK.
h2
Number of Planets
p
3
document.getElementById("123")
document.getElementsByTagName("input")
document.getElementsByClassName("redtext")
document.querySelector("#123");
document.querySelectorAll("input");
document.querySelectorAll(".redtext");
innerHTML nodeName firstChild lastChild childNodes previousSibling nextSibling
element.attributeName = "HTMLAttributeValue"; element.style.styleName = "cssStyleValue";
document.createElement(element); document.removeChild(element); document.appendChild(element); document.replaceChild(oldElement, newElement);
<p id="output">Output:</p>
var elements = document.getElementsByTagName("html");
// The previous line can also be written as
// var element = document.querySelector("html");
var para = document.getElementById("output");
para.innerHTML = elements[0].nodeName;
// If the querySelector method is used, do this
// para.innerHTML = elements.nodeName;
var elements = document.getElementsByTagName("li");
// The previous line can also be written as
var elements = document.querySelectorAll("li");
var para = document.getElementById("output");
para.innerHTML = elements.length;
<body></body>Mountains Example. Build this table entirely by using JavaScript:
| Name | Height | Country |
|---|---|---|
| Everest | 8850 | Nepal/China |
| Denali | 6194 | United States |
| Kilimanjaro | 5895 | Tanzania |
var mountains = [
{ mountain: "Everest",
height: 8850,
country: "Nepal/China" },
{ mountain: "Denali",
height: 6194,
country: "United States" },
{ mountain: "Kilimanjaro",
height: 5895,
country: "Tanzania" } ];
Define mountains as a global variable at the beginning of the script. Answer:
---------------------------------------------
<!DOCTYPE html>
<!-- Mountains Example. Source code file: index.html -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Mountains Example</title>
<script src="script.js"></script>
</head>
<body></body>
</html>
---------------------------------------------
// Mountains Example. Source code file: script.js
function init( ) {
var bodyObj = document.querySelector("body");
var tableObj = document.createElement("table");
bodyObj.appendChild(tableObj);
// Construct first row of table.
var trObj = document.createElement("tr");
tableObj.appendChild(trObj);
// Add th tag for Montain column.
var thObj = document.createElement("th");
thObj.innerHTML = "Mountain";
trObj.appendChild(thObj);
// Add th tag for Height column.
thObj = document.createElement("th");
thObj.innerHTML = "Height";
trObj.appendChild(thObj);
// Add th tag for Country column.
thObj = document.createElement("th");
thObj.innerHTML = "Country";
trObj.appendChild(thObj);
// Add data to table.
for(mount of mountains) {
// Create new table row.
trObj = document.createElement("tr");
tableObj.appendChild(trObj);
// Create td tag for mountain.
tdObj = document.createElement("td");
tdObj.innerHTML = mount.mountain;
trObj.appendChild(tdObj);
// Create td tag for height.
tdObj = document.createElement("td");
tdObj.innerHTML = mount.height;
trObj.appendChild(tdObj);
// Create td tag for country.
tdObj = document.createElement("td");
tdObj.innerHTML = mount.country;
trObj.appendChild(tdObj);
}
}
window.addEventListener("load", init);
---------------------------------------------