The most basic DOM manipulations (Part 1)

Tram Ho

Following this series, I want to mention the power of dynamic HTML programming through the DOM. With the DOM and JavaScript getting all the power needed to create dynamic HTML:

  • JavaScript can change all HTML elements in the page
  • JavaScript can change all HTML attributes in the page
  • JavaScript can change all CSS styles in the page
  • JavaScript can remove existing HTML elements and attributes
  • JavaScript can add new HTML elements and attributes
  • JavaScript can respond to all HTML events present in the page
  • JavaScript can generate new HTML events in the page ….

To be able to do so we need to continue tracking the next section. I will guide you to affect each element of HTML through the DOM.

DOM Tree

What is a root node? The HTML document is the root node, the starting point for building a DOM tree

Element node: <body> <ul> <tr> <head> <title> <a> <div> <p> … are allowed sub branches.

Attribute node: a child of the element node, this is a leaf, without a branch. Attribute is the attribute of DOM elements. The attribute attribute indicates the characteristics of that DOM element => the body tag has a height of 800px.

Text node:

<h1> <a> <span> <li> <td> <th> text buttons, this is the leaf, there is no branch. Indicates the content is readable (human readable), because it is of course there must be something to read. In addition, the DOM also has a comment node. We have an HTMl representation of 1 DOM structure as follows:

Basic DOM manipulation steps

So now I will practice selecting the right HTML element to create the effects as mentioned above.

Step 1: Ways to choose the DOM.

  1. Pass element ID => document.getElementById (‘test-id’)
  2. By element’s name => document.getElementsByTagName (‘p’) [0]
  3. By element’s class name => document.getElementsByClassName (‘class-name’) [0]
  4. Via the name of the element => document.getElementsByName (‘test-name’)
  5. Through specific depth => document.querySelectorAll (“div p # test-id”)
  6. Select both 1 array of multiple elements => document.querySelectorAll (“div p”) => contains 1 array of 2 elements <p>

Step 2: How to change values ​​in the DOM

Assign new values ​​to the text node

And this code means that find the tag with id = “test-id” and assign the internal HTML content of this tag as the words “The value has been changed”.

Change the styling element

We can use it to style an HTML element by selecting that element and using the code below

Example:

The above example changed the color and width of the div tag element

Change element properties in the DOM

For example, if the HTML <img> element is known to have the src attribute to indicate the image URL that the tag displays, knowing that it has an attribute with the name src, after getting this part from the DOM, you have Readable, assigned properties and it will update the HTML page if assigned

Similarly, change the href attribute in the <a> link element

There are other ways to work:

element.appendChild (newNode) Add newNode element to element element it becomes the last child element of the element

element.insertBefore (newNode, node1) Insert the newNode element in front of node1

element.replaceChild (newNode, oldNode) Replace oldNode element with newNode element

The above code changes h1 with the demo id to create 1 content as follows:

Similarly, we have methods

removeChild () is used to delete a child.

Now delete the part with id “nested”.

replaceChild () to replace a certain child element with a new component.

Summary

In this part, we have become familiar with the most basic or commonly used DOM manipulation:

  1. DOM Element: is responsible for accessing certain HTML tags through properties such as class name, id, and name of HTML tag.
  2. DOM HTML: Specialized in handling issues related to the content and attributes of HTML tags
  3. DOM CSS: is responsible for changing the CSS format of the HTML tags.
  4. DOM Event: is responsible for assigning events like onclick (), onload () to HTML tags

Coming to the next part 2, I will take a closer look at more advanced and complex DOM techniques:

  1. DOM EventListener: is responsible for listening for events acting on that HTML tag.
  2. DOM Navigation: used to manage and manipulate HTML tags, showing the parent-child relationship of HTML tags.
  3. DOM Node, Nodelist: is responsible for manipulating HTML through objects (Object).

Many thanks for the interest from the readers! Hope to receive more comments and share more from you.

Reference source

Javascript & DOM: https://xuanthulab.net/cap-nhat-thuoc-tinh-phan-tu-dom-javascript.html

Thach Pham: https://thachpham.com/web-development/javascript/dom-in-javascript-can-ban.html#ftoc-heading-9

Some useful DOM manipulations: https://techblog.vn/mot-so-cau-lenh-javascript-huu-ich-de-thao-tac-voi-dom

Share the news now

Source : Viblo