Table of Contents
What are HTML tags
Example
Use replaceWith()
grammar
Use querySelector()
Welcome
Home Web Front-end HTML Tutorial How to change text inside all HTML tags using JavaScript

How to change text inside all HTML tags using JavaScript

Aug 26, 2023 pm 01:57 PM
javascript Change text within html tags

How to change text inside all HTML tags using JavaScript

In this article, we will use JavaScript to perform the task of changing the text within all HTML tags. Let’s dive into this article to learn more about changing text in all HTML, but first, let’s define HTML tags.

What are HTML tags

HTML tag is a piece of markup language used to indicate the beginning and end of HTML elements in an HTML document. HTML tags are the building blocks of HTML elements that help web browsers convert HTML documents into web pages.

For better understanding use JavaScript to change the text inside all HTML tags. Let's look at the following example.

Example

In the example below, we will create a simple web page where we will change the text by running a script.

<!DOCTYPE html>
<html>
   <body style="text-align:center;">
      <h2>
         Click on the button to change the text
      </h2>
      <label id = "tutorial">
         Welcome to Tutorialspoint
      </label>
      <br>
      <button onclick="changetext()">
         Click Here!
      </button>
      <script>
         function changetext() {
            var x = document.getElementById("tutorial");
            if (x.innerHTML === "Welcome to Tutorialspoint") {
               x.innerHTML = "The Best E-way Learning";
            } else {
               x.innerHTML = "Welcome to Tutorialspoint";
            }
         }
      </script>
   </body>
</html>
Copy after login

When the script executes, it will generate an output consisting of text and a clicked button. If the user clicks the button, the text will change and another text will appear on the web page.

Use replaceWith()

The

Element.replaceWith() method replaces the objects in this Element's parent's children list with a set of Node or string objects. Inserts the equivalent text node for a String object.

grammar

The following is the syntax of replaceWith() -

ChildNode.replaceWith(Node);
Copy after login

Example

Consider the following example where we use replaceWith() to change the text within a tag.

<!DOCTYPE html>
<html>
   <body>
      <h2 id="tutorial">
      <span class="quotation">“</span>
      Lost in the ave of you
      <span class="quotation">”</span>
      </h2>
      <script>
         const changed = document.getElementById('tutorial');
         changed.childNodes[2].replaceWith('Welcome');
      </script>
   </body>
</html>
Copy after login

When you run the above script, an output window will pop up showing the text on the web page replaced with the original text used in the script.

Use querySelector()

When one or a set of selectors is provided, the Document method querySelector() returns the first Element in the document that matches them. Returns Null if there is no match.

grammar

The following is the syntax of querySelector()

querySelector(selectors)
Copy after login

Example

Execute the following code, we use querySelector() to change the text in the HTML tag.

<!DOCTYPE html>
<html>
   <body>
      <h1 id="Welcome">Welcome</h1>
      <script>
         document.querySelector("h1").textContent = 'Tutorialspoint';
      </script>
   </body>
</html>
Copy after login

When scripts are executed, they will produce an output containing the text from the web page replaced with the original text used in the markup.

The above is the detailed content of How to change text inside all HTML tags using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to change the storage location of wallpaper engine? How to set the save path in wallpaper engine How to change the storage location of wallpaper engine? How to set the save path in wallpaper engine Mar 13, 2024 pm 12:40 PM

Many users like to download various wallpapers and videos on WallpaperEngine. Over time, they will find that more and more wallpapers are downloaded, resulting in insufficient hard disk space. At this time, the storage location of WallpaperEngine can be changed to reduce the space occupied. So let’s take a look at how to change the save path for wallpaperengine. Step 1: Click Settings under steam in the upper left corner to open the following interface. Step 2: Click Download to find the "Steam Library Folder" under the content library, and click Open above. Step 3: Click Add Library Folder, select the path you want to change to, and after adding it, right-click on the default column.

How to change the font color of win7 desktop icons How to change the font color of win7 desktop icons Jan 02, 2024 pm 11:17 PM

The default desktop icon font of win7 is generally white. If we use a white desktop background, the desktop icon text may not be visible. At this time, we can customize the desktop font color through the advanced appearance settings in the personalization settings. The following is Let’s take a look together. Tutorial on changing the font color of win7 desktop icons 1. Right-click a blank space on the desktop and open the "Personalization" settings. 2. Under Theme, we can directly select the desired theme to change the font color of desktop icons. 3. If you are not satisfied with these themes, you can also turn on the "Window Color" as shown in the picture. 4. Click "Advanced Appearance Settings" below 5. Change the "Project" at the icon location to "Desktop" 6. Then you can change various attributes such as font color and size in the red box

Step-by-step guide to changing background color with Eclipse Step-by-step guide to changing background color with Eclipse Jan 28, 2024 am 08:28 AM

Teach you step by step how to change the background color in Eclipse, specific code examples are required Eclipse is a very popular integrated development environment (IDE) that is often used to write and debug Java projects. By default, the background color of Eclipse is white, but some users may wish to change the background color to suit their preference or to reduce eye strain. This article will teach you step by step how to change the background color in Eclipse and provide specific code examples. Step 1: Open Eclipse First

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to change region settings on xbox store How to change region settings on xbox store Dec 24, 2023 pm 08:53 PM

When the game you want to buy is not available, you can purchase it by changing the region. Do any players know how to change the region settings in the Xbox store? So let’s take a look at the introduction to changing the region settings in the Xbox store! Xbox store region settings: 1. Open windows settings - select time and language. 2. Select the region - the default should be China - select other countries and regions. 3. Select other countries and regions - enter the store - the store prompts you to refresh the content.

Teach you how to modify the temporary file location of Win7 Teach you how to modify the temporary file location of Win7 Jan 04, 2024 pm 11:25 PM

The temp folder is our temporary file storage location. The system will save temporary files to this folder. If there are too many temporary files, especially when the temp folder is on the system disk, it is likely to affect the system running speed. We can solve the problem by changing the temp location. Let’s take a look below. Tutorial on changing the location of win7temp 1. First, right-click "Computer" and open "Properties" 2. Click "Advanced System Settings" on the left 3. Click "Environment Variables" below 4. Select "temp" and click "Edit" 5. Then change Just change the "Variable Value" to the path that needs to be changed.

Teach you how to modify the startup sequence of win7 Teach you how to modify the startup sequence of win7 Jan 04, 2024 pm 09:06 PM

Properly setting the startup sequence of win7 can make our computer boot faster, but many friends don’t know how to change the startup sequence. In fact, we can change it in the registry editor. Let’s take a look at it with the editor. A specific method. Tutorial on changing the win7 startup sequence 1. Search for "Run" in the lower left corner. 2. Open the "Run" program. 3. Enter “regedit” and press Enter to confirm. 4. Find the following path "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServiceGroupOrder" and right-click to modify the icon "list" file. 5. The position shown in the picture is from top to bottom.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles