Manipulating HTML using JavaScript

Selecting HTML document with JavaScript

Given the following HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Website</title>
</head>
<body>
    <h1 id="title">Hello</h1>
    <input type="checkbox">
    <button class="btn">Click Me</button>

    <ul id="list">
        <li class="item"><a href="https://www.google.com">Google</a></li>
        <li class="item">Second</li>
        <li class="item">Third</li>
    </ul>

    
</body>
    <footer> 
        <script src="index.js" charset="utf=8"></script>
    </footer>
</html>
  • getElementsByTagName searches for elements with particular tag name. For example, document.getElementsByTagName("li) gives array of list items. document.getElementsByTagName("li")[2].style.color = "purple" to change the color.
  • document.getElementsByClassName("btn")[0] gives the first item.
  • document.getElementById just returns single item because ids are unique in a document.
  • document.querySelector just returns a single item, but takes a CSS selector as input (element/class/id).
    • Example: document.querySelector("#title"). Use . for class.
  • Hierarchical selectors: document.querySelector("li a"). Remember the space and read from right to left.
  • Combining selectors (things that occur in the same element): document.querySelector("li.item")
If selector matches more than one object, then the first match is returned. If one wants all matches, use querySelectorAllquerySelector and its plural variety allows for complex selections. getElement methods are more broad. Generally, querySelector occurs more often.

Manipulating styles using JavaScript

JavaScript naming conventions are camel-cased. When trying to change the css styles, the property names are different from css. E.g. for changing font size:
document.querySelector("h1").style.fontSize = "3rem".

Separation of concerns

It is not good practice to change style using JavaScript, whereas styles should be inside CSS. But to dynamically change the style, one needs JavaScript. Class list is a property of every DOM object.
  • document.querySelector("button").classList.
  • Add classes: document.querySelector("button").classList.add("invisible"); This way one can create a separate style for the invisible class. This way all styles are inside stylesheet. One can also remove classes. There is also a toggle method.

Manipulating text using selected items from DOM

Instead of innerHTML, use the textContentinnerHTML gives the HTML in between the element tags. If there is a child tag, one gets back the HTML including child tag.

Manipulating attributes of an element

  • list of attributes: document.querySelector("a").attributes gives a list of all attributes. document.querySelector("a").getAttribute("href"). To change it, use setAttribute which takes two parameters, setAttribute("href","https://www.bing.com");

References

Complete Web Development Bootcamp in Udemy.

Comments

Popular posts from this blog

QTreeView and QTableView dynamic changes

C++ strings and string_view