How To Change The Text Of An Element Using JavaScript?

Discover how to easily change the text of any element on your webpage using JavaScript.

This article will guide you through the use of the textContent and innerHTML properties to update text in real-time, with clear examples and explanations.

Experience the impact of JavaScript in making your website more engaging with dynamic text updates in real-time.

Changing text in JavaScript

The HTML Document Object Model (DOM) allows JavaScript to modify or change the content of HTML elements.

JavaScript is a programming language that allows you to add interactivity to your website.

One of the things you can do with JavaScript is change the text on your webpage.

There are two main ways to do this: using the textContent property or the innerHTML property.

The textContent property lets you change the text of an element, without changing any of its HTML structure.

On the other hand, the innerHTML property lets you change not only the text, but also the HTML content of an element.

How to change a text with JavaScript?

To change the text of an element using JavaScript, you can use the textContent or innerHTML properties.

Using textContent property

Here’s an example of how to change the text of an element with the id ITSC using the textContent property:

document.getElementById("id").textContent = "New text!";

Here’s the complete code:

<!DOCTYPE html>
<html>

<head>
	<title>
		How to change the text using JavaScript ?
	</title>
	<style>
		body {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			height: 90vh;
			margin: 0;
		}
	</style>
</head>

<body>
	
	<h1 style="color:blue;">
		Itsourcecode
	</h1>

	<h4>
		Click the button to change
		the text.
	</h4>

	<p id = "ITSC"> 
		Welcome to Itsourcecode
	</p>
	
	<br>
	
	<button onclick="itsourcecode()">
		Try to click me!
	</button>

	<script>
		function itsourcecode() { 
			document.getElementById('ITSC').textContent 
				= 'Itsourcecode.com is designed to help new and experienced programmers expand their knowledge and improve their programming skills';
		}
	</script>
</body>

</html>

Output:

Using innerHTML property

If you want to change the HTML content of an element, you can use the innerHTML property.

Here’s an example that changes the text of an element with id=”ITSC” using the innerHTML property:

document.getElementById("id").innerHTML = new HTML

Here’s the complete code:

<!DOCTYPE html>
<html>

<head>
	<title>
		How to change the text using JavaScript?
	</title>
	<style>
		body {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			height: 90vh;
			margin: 0;
		}
	</style>
</head>

<body>
	
	<h1 style="color:blue;">
		Welcome to Itsourcecode
	</h1>

	<h4>
		Click the button to change
		the text.
	</h4>

	<p id = "ITSC"> ✅
		Welcome to Itsourcecode
	</p>
	
	<br>
	
	<button onclick="itsourcecode()">
		Try to Click Me!
	</button>

	<script>
		function itsourcecode() {
			document.getElementById('ITSC').innerHTML 
				= 'Itsourcecode.com is designed to help new and experienced programmers expand their knowledge and improve their programming skills';
		}
	</script>
</body>

</html>

Output:

However, keep in mind that using innerHTML can be a security risk if you’re not careful, as it can introduce cross-site scripting (XSS) vulnerabilities.

So, if you’re only changing text and not adding any HTML elements, it’s generally safer to use textContent.

Conclusion

In conclusion, this article provides a straightforward guide to changing the text of webpage elements using JavaScript.

We have discussed the two primary methods, the textContent and innerHTML properties, both of which allow dynamic text updates.

While textContent is recommended for safer text-only changes, innerHTML enables alterations to HTML content as well.

We are hoping that this article provides you with enough information that helps you understand the JavaScript change text.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. Changing text in JavaScript. Read the ‘Changing text in JavaScript’ section for the details and code.
  2. How to change a text with JavaScript. Read the ‘How to change a text with JavaScript?’ section for the details and code.
  3. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment