codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

59 / 65 topics
57JavaScript DOM Introduction58JavaScript Selecting DOM Elements59JavaScript Changing HTML & CSS60JavaScript Events61JavaScript addEventListener62JavaScript and JSON63JavaScript Fetch API64JavaScript Local Storage65JavaScript Regular Expressions
Tutorials/JavaScript/JavaScript Changing HTML & CSS
🌐JavaScript

JavaScript Changing HTML & CSS

Updated 2026-05-12
30 min read

JavaScript Changing HTML & CSS

In this tutorial, we'll explore how to manipulate HTML content and CSS styles dynamically using JavaScript. Understanding these techniques is crucial for creating interactive web applications that respond to user actions in real-time.

Introduction

The Document Object Model (DOM) provides a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. By manipulating the DOM, you can dynamically update the HTML content and CSS styles of your webpage without needing to reload the page.

In this tutorial, we'll cover several methods to modify:

  • Text content
  • innerHTML
  • Attributes
  • Styles

These techniques will allow you to create interactive web applications that respond to user actions in real-time.

Modifying Text Content

The simplest way to change text content is by using the textContent property. This property allows you to get or set the text content of an element and its descendants.

Example 1: Changing Text Content

JavaScript
1// script.js
2document.getElementById('demo').textContent = 'Hello, World!';
HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>Change Text</title>
7</head>
8<body>
9 <p id="demo">Original text</p>
10 <script src="script.js"></script>
11</body>
12</html>
Output

Using innerHTML

The innerHTML property allows you to get or set the HTML content of an element. This is useful when you need to insert HTML elements dynamically.

Example 3: Inserting HTML Content

JavaScript
1// script.js
2document.getElementById('demo').innerHTML = '<strong>Bold Text</strong>';
HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>Insert HTML</title>
7</head>
8<body>
9 <div id="demo">Original text</div>
10 <script src="script.js"></script>
11</body>
12</html>
Output

Modifying Attributes

Attributes are additional pieces of information that can be added to HTML elements. You can modify these attributes using the setAttribute, getAttribute, and removeAttribute methods.

Example 5: Changing an Attribute

JavaScript
1// script.js
2document.getElementById('demo').setAttribute('href', 'https://www.example.com');
HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>Change Attribute</title>
7</head>
8<body>
9 <a id="demo" href="#">Link</a>
10 <script src="script.js"></script>
11</body>
12</html>
Output

Modifying Styles

You can modify the CSS styles of an element using the style property. This allows you to change individual style properties dynamically.

Example 7: Changing a Single Style Property

JavaScript
1// script.js
2document.getElementById('demo').style.color = 'red';
HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>Change Style</title>
7</head>
8<body>
9 <p id="demo">Original text</p>
10 <script src="script.js"></script>
11</body>
12</html>
Output

Practical Example

Let's create a simple interactive application that changes the text, style, and attribute of an element when a button is clicked.

JavaScript
1// script.js
2document.getElementById('changeButton').addEventListener('click', function() {
3 const demoElement = document.getElementById('demo');
4
5 // Change text content
6 demoElement.textContent = 'Text has been changed!';
7
8 // Change style
9 demoElement.style.color = 'green';
10 demoElement.style.fontSize = '24px';
11
12 // Change attribute
13 demoElement.setAttribute('href', 'https://www.example.com');
14});
HTML
1<!-- index.html -->
2<!DOCTYPE html>
3<html lang="en">
4<head>
5 <meta charset="UTF-8">
6 <title>Interactive Example</title>
7</head>
8<body>
9 <a id="demo" href="#">Original Link</a>
10 <button id="changeButton">Change Text and Style</button>
11 <script src="script.js"></script>
12</body>
13</html>
Output
[Original Link](#)
[Text has been changed!](https://www.example.com)

Summary

  • Text Content: Use textContent to get or set the text content of an element.
  • innerHTML: Use innerHTML to insert HTML content into an element.
  • Attributes: Use setAttribute, getAttribute, and removeAttribute to modify attributes of an element.
  • Styles: Use the style property to change individual CSS styles dynamically.

What's Next?

In the next tutorial, we'll explore how to handle events in JavaScript. Events are actions that occur when a user interacts with a webpage, such as clicking a button or typing in an input field. Understanding and handling events is essential for creating interactive web applications.

Stay tuned!


PreviousJavaScript Selecting DOM ElementsNext JavaScript Events

Recommended Gear

JavaScript Selecting DOM ElementsJavaScript Events