Practice finding elements on the DOM using JavaScript.
- Find HTML elements using
class
andid
attributes - Store JavaScript objects in variables
et get launchbook-js
cd launchbook-js
open index.html
atom .
For this lesson, there are some fake ads along the right hand side of the page. Ad blocking extensions may detect these as actual ads and hide them, so it is recommended that you turn off ad blocking extensions while working on this lesson.
Open the JavaScript console in Chrome so that you can follow along with the code. You can open the JavaScript console by navigating the menu bar to View -> Developer -> JavaScript Console
or with the shortcut cmd + option + j
.
The start of this challenge provides examples - things for you to do on your own to practice the concept. You should try out each example and exercise in the browser console. Once you complete an exercise, add your code to the the solutions.js
file. (This file is not actually run. The file is simply a place to record your answers).
JavaScript lets us use HTML class and id attributes to find elements that are in the DOM (on the page). The most basic way to find something with JavaScript is via the HTML element type (aka tag name).
Example: Find the status update form. (Paste this into the developer console and hit return.)
document.getElementsByTagName('form')[0];
Exercise 1): Find the top navbar, using a query for the HTML element type. The navbar's type is <nav>
.
We can also find an element by its id.
Example: Find the right sidebar by its id.
document.getElementById('sidebar-right');
Exercise 2): Find the sidebar on the left by using its id and set it equal to the variable sideBar.
We can also find elements by their class.
Example: Find the 'Favorites' section of the sidebar by using its class.
document.getElementsByClassName('favorites')[0];
Exercise 3): Find the 'Pages' and 'Groups' sections of the sidebar by using their class.
We can scope our queries to child elements by chaining functions.
Example: Find all of the sections of the sidebar.
document.getElementById('sidebar-left').getElementsByTagName('div');
Exercise 4): Using the sideBar
variable you just declared, set the text of the 'Favorites' h5
to say "Least Favs".
We can set the CSS properties of elements on the page via the style
function.
Example: Hide the status update form.
let statusUpdateForm = document.getElementsByClassName('status-update')[0];
statusUpdateForm.style.visibility = 'hidden';
Exercise 5): Find the first of the ads in the sidebar and hide it
We can change the visibility back to what it was, initially.
Example: Show the status update form.
let statusUpdateForm = document.getElementsByClassName('status-update')[0];
statusUpdateForm.style.visibility = 'visible';
Exercise 6): Set the visibility on the ad that you hid in the previous exercise to make it visible again.
We can use the setAttribute
function to modify the value of an element's attribute.
Example: Change the placeholder of the status update form's <textarea>
placeholder
to "What's up?!"
document.getElementsByTagName('textarea')[0].placeholder = "What's up?!"
Exercise 7): Use setAttribute
to change src
attribute of one of the ads in the sidebar.
Here's an image source if you need one: http://placebear.com/200/300
.
We can use the innerHTML
property of an element to change the text content of that element.
Example: Change the text of our "Ad Slot" to "Goat Ads".
document.getElementsByClassName('ads')[0].innerText = "Goat Ads"
Exercise 8: Find Sam's post and change its text to something incredible.
Example: Add the .active
class to the 'Events' link in the left favorites sidebar and see it change color.
let favorites = document.getElementsByClassName('favorites')[0];
let eventsLink = favorites.getElementsByTagName('li')[2];
eventsLink.className = eventsLink.className + ' active'
Exercise 9): Find the first post and add the .post-liked
class to it to see the background turn blue.
Example: Remove the .active
class from the 'Events' link in the left
sidebar to see the color change back.
let favorites1 = document.getElementsByClassName('favorites')[0];
let eventsLink1 = favorites1.getElementsByTagName('li')[2];
eventsLink1.classList.remove('active');
Exercise 10): Find the second post and add the .post-shared
class to the li
containing the text "Share" to watch it turn red.