This HTML file includes the JavaScript code that demonstrates how to access and manipulate DOM elements with class names and ids. The code modifies the innerHTML of the elements with the class "example" and adds/removes/toggles classes on the element with the id "myId".
It should be noted that if you use className property to change classes it will replace all the existing classes with the new classes, while if you use classList.add() or classList.remove() it will only add or remove the class you specify.
Accessing and modifying the DOM:
<h1 div id="myId"> Hello Class </h1>
// Access an element by its ID
let element = document.getElementById("myId");
// Modify the innerHTML of an element
element.innerHTML = "New Content";
<h1 div id="myId"> Hello Class </h1>
// Access an element by its class name
let elements = document.getElementsByClassName("myClass");
// Access an element by its tag name
let elements = document.getElementsByTagName("p");
Traversing the DOM:
Copy code
// Access the parent node of an element
let parent = element.parentNode;
// Access the children of an element
let children = element.children;
// Access the first child of an element
let firstChild = element.firstChild;
// Access the last child of an element
let lastChild = element.lastChild;
Creating and inserting new elements:
// Create a new element
let newElement = document.createElement("div");
// Set the innerHTML of the new element
newElement.innerHTML = "New Content";
// Append the new element to an existing element
element.appendChild(newElement);
// Insert the new element before an existing child element
To assign a divid to an HTML element, you can use the id attribute. For example:
<div id="myId">This is my div element</div>
You can also assign an id attribute to an element using JavaScript by setting the id property of the element. For example:
let element = document.createElement("div");
element.innerHTML = "This is my div element";
element.id = "myId";
document.body.appendChild(element);
Note that id attribute should be unique in the document.
how to rewrite the innerhtml of an html element by accessing with div
You can rewrite the innerHTML of an HTML element by accessing it using its id and then setting the innerHTML property.
Here is an example of how you can change the innerHTML of a div element with the id of "myId":
let element = document.getElementById("myId");
element.innerHTML = "New Content";
You can also use innerText to change the text inside a div
let element = document.getElementById("myId");
element.innerText = "New Content";
You can also use textContent to change the text inside a div
let element = document.getElementById("myId");
element.textContent = "New Content";
The difference between innerHTML, innerText and textContent is how they handle white space and html tags.
innerHTML will interpret any html tags within the string as actual html, innerText will not display any html tags as text, and textContent is similar to innerText but it includes all text including text within child elements.
Want to print your doc? This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (