Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Chapter 05/Code Samples/ch5_while.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ while (notFound && someArray.length > 0) {
if (someArray[0] === "Louiza") {
console.log("Found her!");
notFound = false;
console.log("false");
} else {
someArray.shift();
}
Expand Down
11 changes: 11 additions & 0 deletions Chapter 10/Code Samples/ch10-accessing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>
<h1 style="color:pink;">Just an example</h1>
<div id="one" class="example">Hi!</div>
<div id="two" class="example">Hi!</div>
<div id="three" class="something">Hi!</div>
</body>
<script>
console.log(document.getElementById("two"));
</script>
</html>
26 changes: 26 additions & 0 deletions Chapter 10/Code Samples/ch10-attributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<html>
<body>
<script>
function changeAttr(){
let el = document.getElementById("shape");
el.setAttribute("style", "background-color:red;border:1px solid black");
el.setAttribute("id", "new");
el.setAttribute("class", "circle");

}
</script>
<style>
div {
height: 100px;
width: 100px;
background-color: yellow;
}
.circle {
border-radius: 50%;
}
</style>
<div id="shape" class="square"></div>

<button onclick="changeAttr()">Change attributes...</button>
</body>
</html>
27 changes: 27 additions & 0 deletions Chapter 10/Code Samples/ch10-classes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<body>
<script>
function disappear(){
document.getElementById("shape").classList.add("hide");
}
</script>
<style>
.hide {
display: none;
}

.square {
height: 100px;
width: 100px;
background-color: yellow;
}

.square.blue {
background-color: blue;
}
</style>
<div id="shape" class="square blue"></div>

<button onclick="disappear()">Disappear!</button>
</body>
</html>
23 changes: 23 additions & 0 deletions Chapter 10/Code Samples/ch10-classes2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<body>
<script>
function change(){
document.getElementById("shape").classList.remove("blue");
}
</script>
<style>
.square {
height: 100px;
width: 100px;
background-color: yellow;
}

.square.blue {
background-color: blue;
}
</style>
<div id="shape" class="square blue"></div>

<button onclick="change()">Change!</button>
</body>
</html>
23 changes: 23 additions & 0 deletions Chapter 10/Code Samples/ch10-classes3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<body>
<script>
function changeVisibility(){
document.getElementById("shape").classList.toggle("hide");
}
</script>
<style>
.hide {
display: none;
}

.square {
height: 100px;
width: 100px;
background-color: yellow;
}
</style>
<div id="shape" class="square"></div>

<button onclick="changeVisibility()">Magic!</button>
</body>
</html>
23 changes: 23 additions & 0 deletions Chapter 10/Code Samples/ch10-click.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- <html>
<body>
<div id="one" onclick="alert('Auch! Stop it!')">Don't click here!</div>
</body>
</html>


<html>
<body>
<script>
function stop(){
alert("Auch! Stop it!");
}
</script>
<div id="one" onclick="stop()">Don't click here!</div>
</body>
</html> -->

<html>
<body>
<div id="one">Don't click here!</div>
</body>
</html>
27 changes: 27 additions & 0 deletions Chapter 10/Code Samples/ch10-colors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<body>
<script>
function rainbowify(){
let divs = document.getElementsByTagName("div");
for(let i = 0; i < divs.length; i++) {
divs[i].style.backgroundColor = divs[i].id;
}
}
</script>
<style>
div {
height: 30px;
width: 30px;
background-color: white;
}
</style>
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
<div id="indigo"></div>
<div id="violet"></div>
<button onclick="rainbowify()">Make me a rainbow</button>
</body>
</html>
33 changes: 33 additions & 0 deletions Chapter 10/Code Samples/ch10-domflow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html>
<body>
<style>
div {
border: 1px solid black;
margin-left: 5px;
}
</style>
<div id="message">Bubbling events</div>
<div id="output">
1
<div>
2
<div>
3
<div>
4
<div>5</div>
</div>
</div>
</div>
</div>
<script>
function bup() {
console.log(this.innerText);
}
let divs = document.getElementsByTagName("div");
for (let i = 0; i < divs.length; i++) {
divs[i].addEventListener("click", bup);
}
</script>
</body>
</html>
16 changes: 16 additions & 0 deletions Chapter 10/Code Samples/ch10-event-listener.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<body>
<script>
window.onload = function() {
document.getElementById("square").addEventListener("click", changeColor);
}
function changeColor(){
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);
this.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
}
</script>
<div id="square" style="width:100px;height:100px;background-color:grey;">Click for magic</div>
</body>
</html>
12 changes: 12 additions & 0 deletions Chapter 10/Code Samples/ch10-new-element.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<body>
<script>
function addRandomNumber(){
let el = document.createElement("p");
el.innerText = Math.floor(Math.random() * 100);
document.body.appendChild(el);
}
</script>
<button onclick="addRandomNumber()">Add a number</button>
</body>
</html>
8 changes: 8 additions & 0 deletions Chapter 10/Code Samples/ch10-selectionelements.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
<h1>Welcome page</h1>
<p id="greeting">
Hi!
</p>
</body>
</html>
10 changes: 10 additions & 0 deletions Chapter 10/Code Samples/ch10-this.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<script>
function reveal(el){
console.log(el.parentElement);
}
</script>
<button onclick="reveal(this)">Click here!</button>
</body>
</html>
16 changes: 16 additions & 0 deletions Chapter 10/Code Samples/ch10-toggle-visibility.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<body>
<script>
function toggleDisplay(){
let p = document.getElementById("magic");
if(p.style.display === "none") {
p.style.display = "block";
} else {
p.style.display = "none";
}
}
</script>
<p id="magic">I might disappear and appear.</p>
<button onclick="toggleDisplay()">Magic!</button>
</body>
</html>
19 changes: 19 additions & 0 deletions Chapter 10/Code Samples/ch10-treasure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<body>
<h1>Let's find the treasure</h1>
<div id="forest">
<div id="tree1">
<div id="squirrel"></div>
<div id="flower"></div>
</div>
<div id="tree2">
<div id="shrubbery">
<div id="treasure"></div>
</div>
<div id="mushroom">
<div id="bug"></div>
</div>
</div>
</div>
</body>
</html>
30 changes: 30 additions & 0 deletions Chapter 11/Code Samples/ch11-animate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<style>
div {
background-color: purple;
width: 100px;
height: 100px;
position: absolute;
}
</style>
<body>
<button onclick="toTheRight()">Go right</button>
<div id="block"></div>

<script>
function toTheRight() {
let b = document.getElementById("block");
let x = 0;
setInterval(function () {
if (x === 600) {
clearInterval();
} else {
x++;
b.style.left = x + "px";
}
}, 2);

}
</script>
</body>
</html>
46 changes: 46 additions & 0 deletions Chapter 11/Code Samples/ch11-dragdrop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html>
<head>
<style>
.box {
height: 200px;
width: 200px;
padding: 20px;
margin: 0 50px;
display: inline-block;
border: 1px solid black;
}

#dragme {
background-color: red;
}
</style>
</head>

<body>
<div class="box" ondrop="dDrop()" ondragover="nDrop()">
1
<div id="dragme" ondragstart="dStart()" draggable="true">
Drag Me Please!
</div>
</div>
<div class="box" ondrop="dDrop()" ondragover="nDrop()">2</div>
<script>
let holderItem;

function dStart() {
holderItem = event.target;
}

function nDrop() {
event.preventDefault();
}

function dDrop() {
// event.preventDefault();
if (event.target.className == "box") {
event.target.appendChild(holderItem);
}
}
</script>
</body>
</html>
10 changes: 10 additions & 0 deletions Chapter 11/Code Samples/ch11-eventtarget.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<button type="button" onclick="triggerSomething()">Click</button>
<script>
function triggerSomething() {
console.dir(event.target);
}
</script>
</body>
</html>
Loading