Compare commits

...

6 Commits

Author SHA1 Message Date
Joshua Sigona 1ac507813d Provide working solution 5 years ago
Zubair Desai 77a1a7c0f2
Update README.md 6 years ago
Zubair Desai 71f6e6f2eb
Update README.md 6 years ago
Matt Lane 7957874f09 Update README.md 9 years ago
Matt Lane 4168c3ca1d Update app.js 9 years ago
Matt Lane 139798cc07 Update README.md 9 years ago
  1. 17
      README.md
  2. 101
      app.js

@ -2,6 +2,8 @@
Practice manipulating the DOM!
## Part One:
In a separate JS file:
When the page loads:
@ -12,6 +14,14 @@ 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.
@ -22,9 +32,8 @@ Afterwards:
7. When the orange div is moused over, its width doubles. When the mouse moves out of the div, it returns to its original size.
8. When the reset button is clicked - remove the `selected` class from
each `<li>`, change the image to `panic.jpeg`, and add the gray div back to the DOM if it has been removed.
8. When the reset button is clicked - remove the `selected` class from each `<li>` and change the image to `panic.jpeg`.
9. When the a, e, i, o, or u key is pressed, the page alerts the message "I HATE VOWELS!"
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!"

101
app.js

@ -1,7 +1,94 @@
console.log("Javascript is alive!");
window.onload = function() {
document.querySelector('body').addEventListener('keypress', function(e) {
console.log(e)
})
};
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!")
}
})
})

Loading…
Cancel
Save