Git Product home page Git Product logo

dom's Introduction

DOM

Link to lab

Select an Element With JavaScript

  • document.querySelector(selector)
  • document.querySelector('h1');

Delete an Element with JavaScript

  • document.querySelector('h1').remove();

note:The selector is like a query string that lets us find things within an HTML page.

Ask the DOM to Find or "select" an HTML Element or Elements in the Rendered Page

Finding a Node

JavaScript exposes a few ways of finding DOM nodes, either directly or in stages, courtesy of the document object. We will introduce three here, in order from most to least specific: getElementByID(), getElementsByClassName(), and getElementsByTagName().

document.getElementById()

This method provides the quickest access to a node, but it requires that we know a very specific piece of information โ€” its id.

This method can only return one element, since CSS ids are expected to be unique.

  • Syntax:
    • document.getElementById('theIdOfTheElement')
document.getElementsByClassName()

This one is also very commonly used in DOM programming.

This method finds elements by their className. Unlike the previous method, class names do not need to be unique, so this method returns an HTMLCollection of all the elements with the given class. An HTMLCollection is an array-like structure containing a list of elements. You can iterate over an HTMLCollection with a simple for loop.

  • Syntax:
    • document.getElementsByClassName('yourClassNameHere')
document.getElementsByTagName()

You can use this method if you don't know an element's id or class, but you do know its tag name (the tag name is the thing between the <>, e.g., 'div', 'h1', header, article etc.). Since tag names aren't unique, this method also returns an HTMLCollection.

Conclusion

Understanding the tree structure of the DOM helps us navigate all kinds of trees. In subtrees and branches we can find the nodes we need by IDs, class names or tag names, or by using element attributes like children. Once we've selected our elements, we can use JavaScript to manipulate them. By using these techniques, we can start to build a richer user experience.


DOM query selector methods

Link to lesson

we can improve our search when we use document structure (tag, id, class) along with the tree structure of the DOM. It turns out CSS is a great language for expressing those relationships! With the querySelector() and querySelectorAll() methods, we provide one or more CSS selectors as an argument and we get back the matching element or elements. Because they can take a string containing multiple selectors, they allow us to create very specific, complex queries.

querySelector()

The querySelector() method takes one argument, a string of one or more CSS-compatible selectors, and returns the first element that matches.

querySelectorAll()

querySelectorAll() method works a lot like querySelector() โ€” it accepts a string containing one or more selectors as its argument, and it searches starting from the object that it's called on (either document or an element). However, instead of returning the first match, it returns a "NodeList collection" of all matching elements. A NodeList is similar to an HTMLCollection: it is an array-like structure containing, in this case, a list of DOM nodes.

Conclusion

The DOM selection methods document.querySelector() and document.querySelectorAll() are powerful tools for finding the elements we need to update and change. They use the familiar CSS selector syntax and allow us to create very specific queries that give us access to elements in complex DOM trees.

Resources

Create DOM Elements Programmatically

Link to lab

document.createElement()

Creating an element in JavaScript call document.createElement('tagName'), where tagName is the name of any valid HTML tag ('p', 'div', 'span', etc.).

dom's People

Contributors

jayz-lab avatar

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.