Exploring the Powerful Output Possibilities of DOM Manipulation in JavaScript


In JavaScript, the DOM (Document Object Model) provides a way to interact with the HTML and XML documents on a web page. The output possibilities for DOM manipulation in JavaScript are many and varied, but some common examples include:

  1. Changing HTML content: You can use JavaScript to change the HTML content of an element on a web page. For example, you can change the text of a paragraph, the source of an image, or the href attribute of a link.
  2. Modifying CSS styles: You can also use JavaScript to modify the CSS styles of elements on a web page. For example, you can change the background color, font size, or position of an element.
  3. Manipulating the DOM structure: JavaScript can also be used to add, remove, or modify elements in the DOM tree. For example, you can create new elements, delete existing ones, or move elements from one location to another.
  4. Responding to user input: JavaScript can be used to respond to user input, such as mouse clicks or keyboard events. For example, you can create a button that triggers an event when clicked, or you can listen for keyboard events to respond to user input.
  5. Creating animations: JavaScript can be used to create animations on a web page, such as fading in or out an element, or moving an element across the screen.

style

In JavaScript, "style" typically refers to the CSS styling of HTML elements that are manipulated using JavaScript.

JavaScript allows you to modify the style of HTML elements by accessing their style properties. These properties include various CSS styles such as background color, font size, margin, padding, border, and many others. You can use the style property of an element to change its appearance.

For example, if you have an HTML element like this:

 <h1>Tutor Joes</h1>

JavaScript code

const h1 = document.querySelector("h1");
h1.style.color = "blue";
h1.style.backgroundColor = "palegreen";
h1.style.padding = "20px";

This JavaScript code retrieves the first h1 element in the HTML document using the querySelector() method and stores it in the h1 constant.The code then uses the style property of the h1 element to modify its CSS styles dynamically.

Specifically, the first line of the code sets the color of the text inside the h1 element to "blue" by accessing the color style property of the h1 element and setting it to "blue".The second line sets the background color of the h1 element to "palegreen" by accessing the backgroundColor style property of the h1 element and setting it to "palegreen".

Finally, the third line sets the padding of the h1 element to "20px" by accessing the padding style property of the h1 element and setting it to "20px".Overall, this code dynamically modifies the CSS styles of the h1 element, making it have blue text on a pale green background with a 20-pixel padding.

innerHTML

In JavaScript, innerHTML is a property of the HTMLElement interface that allows you to get or set the HTML content inside an element.When you get the value of innerHTML, it returns a string containing the HTML content inside the element, including any nested elements and their content.

For example, if you have an HTML element like this:

 <h1>Tutor Joes</h1>

JavaScript code

const h1 = document.querySelector("h1");
h1.innerHTML = "Learn More <i>Be Smart</i>";

This JavaScript code retrieves the first h1 element in the HTML document using the querySelector() method and stores it in the h1 constant.The code then uses the innerHTML property of the h1 element to modify its HTML content dynamically.

Specifically, the code sets the innerHTML property of the h1 element to a new string that contains the text "Learn More" followed by an italicized "Be Smart" text.The <i> tag in the string is an HTML tag that stands for italic and is used to format text in italics.

Therefore, after executing this code, the content of the h1 element would change to "Learn More Be Smart" with "Be Smart" displayed in italics.Overall, this code uses the innerHTML property of the h1 element to modify its HTML content dynamically, making it display the new text with a stylized italic format.

innerText

In JavaScript, innerText is a property of the HTMLElement interface that allows you to get or set the text content inside an element, excluding any HTML tags.When you get the value of innerText, it returns a string containing the text content inside the element, without any HTML tags.

For example, if you have an HTML element like this:

 <h1>Tutor Joes</h1>

JavaScript code

const h1 = document.querySelector("h1");
h1.innerText = "Learn More <i>Be Smart</i>";

This JavaScript code retrieves the first h1 element in the HTML document using the querySelector() method and stores it in the h1 constant.The code then uses the innerText property of the h1 element to modify its text content dynamically.

Specifically, the code sets the innerText property of the h1 element to a new string that contains the text "Learn More <i>Be Smart</i>".Note that unlike innerHTML, using innerText would not create an italicized text for the "Be Smart" text because it does not parse and display HTML tags. Instead, the entire string, including the <i> tag, will be treated as plain text and displayed as such.

Therefore, after executing this code, the content of the h1 element would change to "Learn More <i>Be Smart</i>", with the <i> tag visible as plain text instead of displaying an italicized "Be Smart" text. Overall, this code uses the innerText property of the h1 element to modify its text content dynamically, making it display the new string of text with the <i> tag treated as plain text.

cloneNode()

In JavaScript, cloneNode() is a method that creates a copy of an HTML element and all its child nodes. The new copy is called a "clone" and is a separate object from the original element, with its own set of properties and attributes.

Here's an example of how to use cloneNode():

 <h1>Tutor Joes</h1>

JavaScript code

const h1 = document.querySelector("h1");
const body = document.querySelector("body");
let cloneH1 = h1.cloneNode(true);
let cloneH2 = h1.cloneNode(false);
body.appendChild(cloneH1);
body.appendChild(cloneH2);

This JavaScript code selects the first h1 element and the body element from the HTML document using the querySelector() method and stores them in the h1 and body constants, respectively. The code then creates two new variables, cloneH1 and cloneH2, and assigns them the result of calling cloneNode() method on the h1 element.

  1. The first cloneH1 variable is created by calling cloneNode() with the argument true. This creates a deep clone of the h1 element, which means that all of its child nodes, including any nested elements, are also cloned.
  2. The second cloneH2 variable is created by calling cloneNode() with the argument false. This creates a shallow clone of the h1 element, which means that only the element itself is cloned, and not its child nodes.
  3. The code then appends both cloneH1 and cloneH2 to the body element using the appendChild() method.
  4. In this example, after executing the code, the body element would contain two new elements: cloneH1 and cloneH2.
  5. cloneH1 would be a deep clone of the original h1 element, including all of its child nodes. Therefore, cloneH1 would be a duplicate of the original h1 element, with the same text content, attributes, and child nodes.
  6. cloneH2, on the other hand, would only be a shallow clone of the original h1 element, without any child nodes. Therefore, cloneH2 would be a new h1 element with the same attributes and properties as the original h1 element, but without any of its child nodes.
  7. Overall, this code demonstrates how to use cloneNode() to create deep and shallow copies of an HTML element, which can be useful when you need to duplicate an element and manipulate its content or style without affecting the original source.

Using cloneNode() can be a useful way to create a copy of an HTML element and its child nodes, which can be manipulated and modified independently of the original element. This can be particularly useful in cases where you want to create dynamic content or modify existing elements without affecting the original source.

setInterval()

In JavaScript, setInterval is a method that repeatedly calls a function or executes a code block at a specified interval of time. The syntax for setInterval is as follows:

<div class="clock"></div>

The function parameter is the name of the function to be called or a block of code to be executed at each interval. The interval parameter is the number of milliseconds between each call of the function.

Here's an example of how to use setInterval():

 <h1>Tutor Joes</h1>

JavaScript code

let clockDiv = document.querySelector(".clock");
clockDiv.style.fontSize = "30px";
function clock() {
  const date = new Date();

  const time = date.getHours() + " : " + date.getMinutes() + " : " + date.getSeconds() + " : " + date.getMilliseconds();
  clockDiv.innerHTML = time;
}

setInterval(clock, 1000);

Using setInterval can be useful for creating animations, updating data on a webpage, or performing any other task that needs to be repeated at regular intervals. However, it's important to use it judiciously, as excessive use of setInterval can lead to performance issues or excessive resource consumption.

  1. Selects an HTML element with the class "clock" using the querySelector() method and stores it in the clockDiv variable.
  2. Sets the font size of the selected element to 30 pixels using the style property and the fontSize attribute.
  3. Defines a function named clock() that uses the Date object to get the current time and format it as a string.
  4. Updates the innerHTML property of the clockDiv element with the current time string using the clock() function.
  5. Calls the clock() function every 1000 milliseconds (1 second) using the setInterval() method.

List of Programs


JS Practical Questions & Answers


JS Practical Project