Compare commits

..

8 Commits

Author SHA1 Message Date
Matt Lane b522853b29 resolve merge conflict 9 years ago
Matt Lane 819788f6ff restructure solution 9 years ago
Matt Lane eb16ee0beb Update app.js 9 years ago
Matt Lane 611180fcf8 add bonus solution 9 years ago
Matt Lane a52d23074a all except bonus 9 years ago
Matt Lane ed0de11490 fix merge conflicts 9 years ago
Matt Lane 56ab9a6297 remove .DS_Store 9 years ago
Colt 953303acef add solution 10 years ago
  1. 12
      README.md
  2. 167
      app.js

@ -2,8 +2,6 @@
Practice manipulating the DOM!
## Part One:
In a separate JS file:
When the page loads:
@ -14,14 +12,6 @@ When the page loads:
3. Create an image tag, set its `src` attribute to `http://49.media.tumblr.com/tumblr_m6qt1rjPSz1rxjzkho1_500.gif`, and append the to the `#greeting` div.
4. Create and add a ul element to the end of the body with a class of "todo-items"
5. Go through the array `['make coffee','eat donut','change diapers','drive to work']` and create an li element for each item e.g. `<li>make coffee</li>`
6. Add each newly created li element to your ul of class "todo-items"
## Part Two:
Afterwards:
4. Add the class of `selected` to an `<li>` when it is clicked. Remove it from any other `li`s as well.
@ -36,4 +26,4 @@ Afterwards:
9. When the 1, 2, 3, 4, 5, 6, 7, 8, 9, or 0 key is pressed, the page alerts the message "I HATE NUMBERZZZ!"
BONUS: If someone types the [Konami Code](https://en.wikipedia.org/wiki/Konami_Code), the page alerts "YOU ARE AN EVENT HANDLER GURUUUUUUUUU!"
BONUS: If someone types the [Konami Code](https://en.wikipedia.org/wiki/Konami_Code), the page alerts "YOU ARE AN EVENT HANDLER GURUUUUUUUUU!"

167
app.js

@ -1,94 +1,73 @@
document.addEventListener('DOMContentLoaded',()=>{
console.log("Javascript is alive!")
var greeting = document.getElementById("greeting")
greeting.innerHTML="Hello, World!"
var listelements = document.getElementsByTagName("li")
for (var i=0;i<listelements.length;i++) {
listelements[i].style="background-color:yellow;"
}
var greeting = document.querySelector("#greeting")
var newimg = document.createElement("img")
newimg.classList.add("img-changer")
newimg.src="http://49.media.tumblr.com/tumblr_m6qt1rjPSz1rxjzkho1_500.gif"
greeting.appendChild(newimg)
var newlist = document.createElement("ul")
newlist.classList.add("todo-items")
var listText = ['make coffee','eat donut','change diapers','drive to work']
for (task of listText) {
var todoItem = document.createElement("li")
todoItem.innerHTML = task
newlist.appendChild(todoItem)
}
document.body.appendChild(newlist)
var clickSelector = (event)=>{
var listitems = document.getElementsByTagName("li");
for (var i=0;i<listelements.length;i++) {
var item = listelements[i];
item.classList.remove("selected")
//console.log("Removed from "+item)
}
event.target.classList.add("selected")
var changer = document.querySelector("img:not(.img-changer)")
changer.src = "./images/"+event.target.innerHTML+".jpeg"
}
listelements = document.getElementsByTagName("li")
for (var i=0;i<listelements.length;i++) {
if (!listelements[i].parentElement.classList.contains("todo-items")) {
listelements[i].addEventListener("click",clickSelector)
}
}
document.getElementById("reset").addEventListener("click",(event)=>{
var selectedItem = document.querySelector(".selected")
selectedItem.classList.remove("selected")
var changer = document.querySelector("img:not(.img-changer)")
changer.src = "./images/panic.jpeg"
})
document.getElementById("ghosting").addEventListener("mouseover",(event)=>{
event.target.remove()
})
document.getElementById("resize").addEventListener("mouseover",(event)=>{
event.target.style.width=event.target.offsetWidth*2+"px"
})
document.getElementById("resize").addEventListener("mouseout",(event)=>{
event.target.style.width=event.target.offsetWidth/2+"px"
})
var keySequence = []
var arrayMatches = (arr1,arr2)=>{
if (arr1.length!=arr2.length) {
return false;
}
for (var i=0;i<arr1.length;i++) {
if (arr1[i]!==arr2[i]) {
return false;
}
}
return true;
}
window.addEventListener("keydown",(event)=>{
var keysListeningFor = [0,1,2,3,4,5,6,7,8,9]
if (keysListeningFor.includes(Number(event.key))) {
alert("I HATE NUMBERZZZ!")
}
keySequence.push(event.key)
if (keySequence.length>10) {
keySequence = keySequence.slice(1,11)
}
var konamiCodeArray = ["up","up","down","down","left","right","left","right","b","a"]
if (arrayMatches(konamiCodeArray,keySequence.map(val=>{
var newval = val.replace("Arrow","")
newval = newval[0].toLowerCase() + newval.substr(1)
return newval
}))) {
alert("YOU ARE AN EVENT HANDLER GURUUUUUUUUU!")
}
})
})
window.onload = function() {
// change greeting
var greeting_div = document.getElementById("greeting");
greeting_div.innerHTML = "Hello, World!";
// set background on lis and add event listener
var list_items = document.querySelectorAll("li");
for (var i = list_items.length - 1; i >= 0; i--) {
list_items[i].style.backgroundColor = "yellow";
list_items[i].addEventListener("click", selectItem);
}
// create and add image
var img = document.createElement('img');
img.src = "http://49.media.tumblr.com/tumblr_m6qt1rjPSz1rxjzkho1_500.gif";
greeting_div.appendChild(img);
// add more event listeners
document.querySelector("#reset").addEventListener("click",resetButtonHandler);
document.querySelector("#ghosting").addEventListener("mouseover", function() { this.remove(); });
document.querySelector("#resize").addEventListener("mouseenter", doubleWidth);
document.querySelector("#resize").addEventListener("mouseleave", resetWidth);
document.querySelector("body").addEventListener("keyup", noDigits);
// BONUS
document.querySelector("body").addEventListener("keyup", konamiCheck);
};
function selectItem() {
var curSelected = document.querySelector(".selected");
if (curSelected) { curSelected.className = ""; }
this.className = "selected";
document.querySelectorAll("img")[1].src = "./images/" + this.innerHTML + ".jpeg";
}
function resetButtonHandler() {
var curSelected = document.querySelector(".selected");
if (curSelected) { curSelected.className = ""; }
document.querySelectorAll("img")[1].setAttribute("src","./images/panic.jpeg");
}
var doubleWidth = function() {
this.style.width = '400px';
};
var resetWidth = function() {
this.style.width = '200px';
};
var noDigits = function(e) {
if (e.keyCode >= 48 && e.keyCode <= 57) {
alert("I HATE NUMBERZZZ!");
}
};
// BONUS
var idx = 0;
var konamiCheck = function(e) {
var konamiCodeKeyCodes = [38,38,40,40,37,39,37,39,66,65];
if (e.keyCode === konamiCodeKeyCodes[idx]) {
idx++;
if (idx === konamiCodeKeyCodes.length) {
alert("YOU ARE AN EVENT HANDLER GURUUUUUUUUU!");
idx = 0;
}
} else {
idx = 0;
}
};
Loading…
Cancel
Save