Giter VIP home page Giter VIP logo

phase-0-the-dom-dev-tools's Introduction

Changing The DOM with DevTools and JavaScript

Learning Goals

  • Demonstrate viewing the DOM through Chrome DevTools
  • Select an element with Chrome DevTools
  • Delete an element with Chrome DevTools
  • Demonstrate that the source is not changed when the DOM is
  • Demonstrate opening the DevTools' JavaScript console
  • Select an element with JavaScript
  • Delete an element with JavaScript
  • Storing node references in variables

Introduction

We've read that updating the DOM will update the browser's rendered page. Let's try this out. We're going to change the DOM in two ways. First, we'll use Chrome's Developer Tools ("DevTools") and our mouse to remove an element from the DOM. Then we'll use the DevTools' JavaScript console to run JavaScript that does the same thing.

Demonstrate Viewing the DOM Through Chrome DevTools

ada-wiki

Let's head back to the Wikipedia page for Ada Lovelace. From this web page, look at the Chrome menu bar at the top of the page. Click on "View", then select "Developer", then "Developer Tools." This will open the Google Developer Tools. Click on the "Elements" tab. Here we see the DOM representation of the HTML source that was loaded into the browser.

Select an Element With Chrome DevTools

Scroll through the Elements panel. You will see some HTML: head tags, body tags, divs, etc. If the body element is collapsed, use the disclosure triangle to expand it. Notice that you can mouse over different elements in the Elements panel and see them highlighted in the browser window. We will continue to drill down until we get can see the DOM element for the page title:

  • locate the div nested inside body that has a class of "mw-page-container";
  • within that div, find the div with a class of "mw-page-container-inner";
  • within that div, find the div with a class of "mw-content-container";
  • within that div, find the main element with an id of "content";
  • finally, within the main element, find the header element.

header-element

Next, locate the h1 element nested inside the "header" element. It should look something like this:

h1-element

Click on the h1 element; you'll see that the title is now highlighted in the rendered browser page. You've now selected an element with the DevTools.

Delete an Element With Chrome DevTools

Press the delete button on your keyboard. The element will vanish from the browser's rendered page.

deleted-header

Demonstrate That the Source is Not Changed When the DOM Is

View the page source. In the Chrome menu bar, click on "View", then select "Developer", then "View Source." You will see that the HTML is just as it always was, with the deleted element still present. (Note that the h1 element is quite far down on the page. To find it more easily, you might want to search the page for its id, "firstHeading".)

html-source

The changes in the DOM do not affect the HTML file on the server. When you think about it, that makes sense. If that were true then anyone could be changing carefully-written HTML. (Of course, in the case of Wikipedia, people can edit the content using Wikipedia's editor, but they aren't directly accessing the underlying HTML.)

The HTML, which lives on the server, is unchanged.

Refresh the page by going to "View" and choosing "Reload this Page." You will be reloading the DOM from the source. The page content will come back.

Demonstrate Opening the DevTools' JavaScript Console

Above, we deleted an element by selecting it in the DevTools and pressing the delete key. We can accomplish the same thing using JavaScript.

In DevTools, click the Console tab. At the prompt, type the word document and press "Enter." You'll get a #document returned. If you hover your mouse over the element, you'll see the entire page highlighted in the browser window. If you expand it, you'll see that it's the exact HTML that you saw in the Elements tab.

Recall that document is an object; as such, it has properties and methods, including a number of different methods that can be used to return elements. Let's find or select an element by speaking JavaScript with the DOM.

Select an Element With JavaScript

In the Console type:

document.querySelector("h1");

This will return something like this:

<h1 id="firstHeading" class="firstHeading mw-first-heading">
  ...
</h1>

Go ahead and click on that disclosure triangle to see more.

When we run document.querySelector('h1');, it returns the DOM node, which is also a JavaScript object. This means that it, in turn, can have methods called on it! This is called method chaining. Let's use method chaining to remove our node from the DOM.

Delete an Element with JavaScript

Now type:

document.querySelector("h1").remove();

The heading is gone! We called document.querySelector('h1') to get the node; we then used method chaining to call the remove() method on the node object. We use dot notation to chain the calls.

Follow the same process we used earlier to verify that the source has not changed. To restore it, simply refresh the page (i.e. reload the DOM).

Storing Node References in Variables

Query methods like querySelector() and the other methods we'll be learning about are expressions: they return a value (specifically, a DOM node). As such, we can save the results of the query into a variable. For example:

const header = document.querySelector("h1");

We now have a reference to that node with a meaningful name; we can simply use header any time we need to refer to our node, rather than always having to look it up with document.querySelector().

You can perhaps imagine how, if we have a program that selects, creates, modifies, or removes a large number of nodes, using this approach will result in code that's easier to read, debug and maintain.

Conclusion

DOM programming is using JavaScript to:

  1. Ask the DOM to find or select an HTML element or elements in the rendered page
  2. Remove the selected element(s) and/or insert new element(s)
  3. Adjust a property of the selected element(s)

In this lesson you just did all that stuff! Learning to duplicate what you can do in DevTools with JavaScript is DOM programming. The next lessons are going to give you more methods for selecting elements and changing them, but you just changed the DOM. High fives are in order.

phase-0-the-dom-dev-tools's People

Contributors

bal360 avatar graciemcguire avatar jlboba avatar lizbur10 avatar maxwellbenton avatar sgharms avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-0-the-dom-dev-tools's Issues

Wikipedia Page Source has changed

Canvas Link

https://learning.flatironschool.com/courses/6190/pages/changing-the-dom-with-devtools-and-javascript?module_item_id=526876#:~:text=Next%2C%20locate%20the%20h1%20element%20nested%20inside%20the%20%22content%22%20div.%20It%20should%20look%20something%20like%20this%3A

Concern

The h1 we are looking for in the example (to delete the "Ada Lovelace" header) is now nested 6 levels deep in the source of Wikipedia's page, requiring clicking through more disclosure triangles, so perhaps the photo provided as a guide should be updated for ease of following along.

Screen Shot 2023-02-04 at 5 43 28 PM

Additional Context

No response

Suggested Changes

No response

The phrase "The return value of" implies that the next term will be a return value

Canvas Link

https://learning.flatironschool.com/courses/5185/pages/changing-the-dom-with-devtools-and-javascript?module_item_id=396156

Concern

It took me a while to figure out what this phrase meant:
The return value of document.querySelector('h1'); is the DOM node,

I was reading "The return value of __" as though it was like:
The return value of 6 for the arithmetic expression 4+2 ...

When one is new to a domain like Document Object Model, it doesn't immediately ring a bell that "document.querySelector('h1')" is a function. (Expression?) My layman's reading perceived it was a return value, which made sense (ha ha!) because I saw that term displayed on my console, so it must have been returned for some reason. (I'd forgotten I'd entered the term myself.)

After several moments of backtracking, thinking again, looking sideways, whatever, I figured out the intended meaning. Yes, it's decipherable, but no, the struggle is not worth it. Simpler phrasing would help me stay productive at consuming good new content. :-)

Additional Context

No response

Suggested Changes

Instead of this:
The return value of document.querySelector('h1'); is the DOM node,

maybe this?
The function document.querySelector('h1');'s return value is the DOM node,
(Or should that be: The expression document.querySelector('h1');'s return value is the DOM node, )

or
The function document.querySelector('h1');'s return value is a DOM node,

or even drop the descriptor:
document.querySelector('h1');'s return value is a DOM node,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.