Skip to content

Instantly share code, notes, and snippets.

@vanderhoop
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save vanderhoop/2dadcb38f522d211ecee to your computer and use it in GitHub Desktop.

Select an option

Save vanderhoop/2dadcb38f522d211ecee to your computer and use it in GitHub Desktop.

Event Listener Sandbox

We started adding event listeners to the divs on the DOM. Now it's your turn to add additional event listeners.

Exercise Objectives

  • get meaningful reps adding event listeners to DOM elements.
  • stop event propagation where appropriate
  • practice self-directed learning

Directions

  1. Add a click event for the <button> element. Each time the button is clicked, it should append a <p> tag with sassy text content to the <body> element.
  2. Add a mouseleave event for the green div. Whenever a the mouse leaves the div, log "You're always pulling away! You're terrified of intimacy!" to the console.
  3. Add a click event listener to the <body> element. When the element is clicked, give it the class of "pleasant".
  • Note: You'll notice that clicking the container will change the background color, but it should only change if the area OUTSIDE the container div is clicked, so make sure you stop the propagation of the click event on the container div.
  1. Add a keypress event (google it) that console logs the letter that's typed and appends an <h2> to the DOM. Make the <h2> read "The User has typed".

Bonus

  1. Create an <h2> tag that reads "Mouse Coordinates" and append it to the body.
  2. Create a <p> tag that reads "X: "
  3. Create a <p> tag that reads "Y: "
  4. Add a mousemove event listener to the body element. It should update the p tags with the x and y coordinates of the mouse with every mouse movement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment