Selecting Elements in the DOM

Selecting Elements in the DOM


Definition of the DOM and Basic Concepts

The DOM, which stands for Document Object Model, represents the structured hierarchy of an HTML document in the form of a tree-like structure, with each HTML element as a node in this tree. The browser generates this tree of nodes, and each node possesses unique properties and methods that can be manipulated using JavaScript.

In the DOM:

  • The document is the root or foundation.
  • The HTML root element is a child of the document.
  • Body and Head are children of the HTML element and are considered siblings.
  • The Title element is a parent to the text node "my text" and is a child of the Head element.
  • The a (anchor) tag and h1 tag are children of the Body element and are also siblings.
  • The href attribute is a child of the a tag.

Selecting Elements in the DOM


1. getElementById

Use this method to select an element by its unique id attribute.

// HTML: <div id="myElement">Content</div>
let element = document.getElementById('myElement');
console.log(element); // Output: <div id="myElement">Content</div>        


2. getElementsByTagName

Use this method to select all elements that match a specified tag name. It returns an HTMLCollection.

// HTML: <p>Paragraph 1</p> <p>Paragraph 2</p>
let paragraphs = document.getElementsByTagName('p');
console.log(paragraphs); // Output: HTMLCollection of all <p> elements        


3. getElementsByClassName

Use this method to select elements by their class name. It returns an HTMLCollection.

// HTML: <div class="myClass">Div 1</div> <div class="myClass">Div 2</div> let elements = document.getElementsByClassName('myClass'); console.log(elements); // Output: HTMLCollection of all elements with class "myClass"        


4. getElementsByName

Use this method to select elements by their name attribute. It returns a NodeList.

// HTML: <input type="text" name="username"> let inputs = document.getElementsByName('username'); console.log(inputs); // Output: NodeList of elements with the name "username"        


5. querySelector

Use this method to select the first element that matches a CSS selector.

// HTML: <div class="myClass" id="unique">Div</div> let element = document.querySelector('.myClass'); console.log(element); // Output: The first element with class "myClass"        


6. querySelectorAll

Use this method to select all elements that match a CSS selector. It returns a static NodeList.

// HTML: <div class="myClass">Div 1</div> <div class="myClass">Div 2</div> let elements = document.querySelectorAll('.myClass'); console.log(elements); // Output: NodeList of all elements with class "myClass"        



要查看或添加评论,请登录

Thi?u Quang Khoa的更多文章

  • JavaScript Interview Questions (Part 4)

    JavaScript Interview Questions (Part 4)

    61 What are the main rules of promise 62 What is callback in callback 63 What is promise chaining 64 What is…

  • JavaScript Interview Questions (Part 3)

    JavaScript Interview Questions (Part 3)

    41 What are the differences between cookie, local storage and session storage 42 What is the main difference between…

  • Top 400 JavaScript Interview Questions (Part 2)

    Top 400 JavaScript Interview Questions (Part 2)

    21 What is the Temporal Dead Zone 22 What is an IIFE (Immediately Invoked Function Expression) 23 How do you decode or…

  • Top 400 JavaScript Interview Questions (Part 1)

    Top 400 JavaScript Interview Questions (Part 1)

    No. Questions 1 What are the possible ways to create objects in JavaScript 2 What is a prototype chain 3 What is the…

  • Javascript: isFunctions (Part 2)

    Javascript: isFunctions (Part 2)

    5. isUndefined Hàm này ki?m tra xem giá tr? có ph?i là hay kh?ng.

    1 条评论
  • Javascript: isFunctions (Part 1)

    Javascript: isFunctions (Part 1)

    1. isNumber Hàm này ki?m tra xem giá tr? có thu?c ki?u và kh?ng ph?i là hay kh?ng.

    2 条评论
  • Javascript Interview Questions

    Javascript Interview Questions

    5 features of javascript What is DOM? Data types in javascript? Difference between let, var, const Difference between…

  • JavaScript Reversed Words

    JavaScript Reversed Words

    All Javascript Reserved Keywords The below list cannot be used as variable names, as they are reserved keywords. Most…

  • Using parseInt() and toString() for conversions

    Using parseInt() and toString() for conversions

    Trong JavaScript, vi?c chuy?n ??i gi?a các ??nh d?ng s? là m?t tác v? ph? bi?n, và có nhi?u ph??ng pháp ?? th?c hi?n…

  • Javascript: Splice vs Slice

    Javascript: Splice vs Slice

    Trong JavaScript, splice và slice là hai ph??ng th?c ???c s? d?ng ?? làm vi?c v?i m?ng, nh?ng chúng có m?c ?ích và cách…

社区洞察

其他会员也浏览了