Git Product home page Git Product logo

js-jquery-dom-reading's Introduction

The DOM

Objectives

  1. Explain what the DOM is
  2. Explain the DOM hierarchy
  3. Explain how to access the DOM with JavaScript

Intro

Let's think about Gmail for a second. When you're looking at your inbox, there are a million actions you can take. You favorite an email and the star turns yellow without a new page loading. You add a tag to an email and the tag also appears without the page reloading. Even opening an email just replaces the inbox content with the email content without loading a new page. All of this is done using JavaScript. But how was the code able to manipulate the HTML? By using, traversing and manipulating the DOM.

The DOM, which stands for Document Object Model, provides a way of manipulating HTML and XML documents. (We call this "way of manipulating" things an Application Programming Interface, or API โ€” but more on those later.) The DOM provides a structural representation of the document in tree form, enabling you to modify its content and visual presentation by using a scripting language such as JavaScript.

The Tree

Dom Structure Tree

Window

  • The highest level of the DOM tree is the window object. Think of the window as the browser window. The window contains the entire DOM document. All components of your HTML file will be accessible from within the window.
  • The window object has a large number of properties that return information about the object. Below are a few examples
window.document;
//returns the entire HTML document

window.innerWidth;
// returns the inner width of the browser window. Open a console in the browser and enter this. Then shrink the browser window and run it again. You should get a different value.

window.innerHeight;
// returns the inner height of the browser window.

Document

  • The document object represents any web page loaded in the browser. The document contains all the nodes that represent the HTML elements of the page. We use the document object to traverse through the HTML and manipulate elements.
  • There are many document properties that allow us to obtain information about the document programmatically.
document.all;
// returns all the nodes inside the document object

document.contentType;
// returns the type of content contained. Most web pages should return "text/html"

document.URL;
// returns the URL of the document object

Below the document are all the nodes representing the HTML or XML elements on the page.

Altering The DOM

We can alter the DOM through several different ways:

  1. Add/remove HTML elements in the page.
  • You can add elements with functions like appendChild.
  • You can remove elements with the similarly named removeChild.
  • Both of these functions can be called on any node in the DOM tree.
  1. Add/remove/change HTML attributes.
  • If you have a DOM node called element, element.attributes gives you access to its attributes.
  • You can remove attributes with removeAttribute.
  1. Add/remove/change CSS styles.
  • You can modify any DOM node's style property.
  1. Listen for key presses or mouse events on Elements.
  • You can add a listener directly using addEventListener. Elements emit an extensive array of events.
  1. Create events in the page.

Selecting Elements

We can select HTML elements in the document with JavaScript and jQuery:

JS:

document.getElementsByTagName("p");
//returns all p tags on a page

// alternatively, we could do
document.querySelectorAll('p');

// the results of these two functions
// are the same in this example, but as
// we'll see later, getElementsByTagName
// and querySelectorAll have different uses

Lots of developers use a library called jQuery for interacting with the DOM. The jQuery library provides a jQuery function (aliased as $), which wraps document.querySelectorAll() and adds a bit of magic.

We could write the above example in jQuery as

$("p");

The DOM will become increasingly important as we use JavaScript and jQuery to manipulate our sites.

Resources

View The DOM on Learn.co and start learning to code for free.

js-jquery-dom-reading's People

Contributors

annjohn avatar ipc103 avatar pletcher avatar victhevenot avatar

Stargazers

 avatar  avatar

Watchers

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

js-jquery-dom-reading's Issues

rewrite

i'm not sure this tells us much, kind of confusing how this is useful in our domain

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.