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. 10
      README.md
  2. 149
      app.js

@ -2,8 +2,6 @@
Practice manipulating the DOM! Practice manipulating the DOM!
## Part One:
In a separate JS file: In a separate JS file:
When the page loads: 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. 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: Afterwards:
4. Add the class of `selected` to an `<li>` when it is clicked. Remove it from any other `li`s as well. 4. Add the class of `selected` to an `<li>` when it is clicked. Remove it from any other `li`s as well.

149
app.js

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