add solution
This commit is contained in:
parent
ca07b4c73d
commit
953303acef
35
app.js
Normal file
35
app.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
var styleListElements = function(){
|
||||||
|
var list_items = document.querySelectorAll("div#essentials > ul > li");
|
||||||
|
for (var i = list_items.length - 1; i >= 0; i--) {
|
||||||
|
// for (initializer, condition, counter)
|
||||||
|
list_items[i].style.backgroundColor = "yellow";
|
||||||
|
list_items[i].addEventListener("click", selectItem);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var selectItem = function(event){
|
||||||
|
// console.log(this.innerHTML);
|
||||||
|
this.classList.add("selected");
|
||||||
|
document.querySelector("img").setAttribute("src","./images/" + this.innerHTML + ".jpeg");
|
||||||
|
};
|
||||||
|
|
||||||
|
var changeGreeting = function(){
|
||||||
|
var greeting_div = document.getElementById("greeting");
|
||||||
|
greeting_div.innerHTML = "Hello Planet earth!";
|
||||||
|
};
|
||||||
|
|
||||||
|
var resetButtonHandler = function() {
|
||||||
|
var list_items = document.querySelectorAll("li");
|
||||||
|
for (var i = list_items.length - 1; i >= 0; i--) {
|
||||||
|
list_items[i].className = "";
|
||||||
|
}
|
||||||
|
document.querySelector("img").setAttribute("src","./images/panic.jpeg");
|
||||||
|
};
|
||||||
|
|
||||||
|
var initialize = function(){
|
||||||
|
changeGreeting();
|
||||||
|
styleListElements();
|
||||||
|
document.querySelector("#reset").addEventListener("click",resetButtonHandler);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.onload=initialize;
|
10
index.html
10
index.html
@ -2,13 +2,9 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>The DOM playground</title>
|
<title>Le DOM playground</title>
|
||||||
<style>
|
<link rel="stylesheet" href="style.css">
|
||||||
.selected{ font-weight: bold;}
|
<script src="app.js" ></script>
|
||||||
</style>
|
|
||||||
<script>
|
|
||||||
console.log("JavaScript is alive");
|
|
||||||
</script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="greeting">
|
<div id="greeting">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user