How to Toggle Text with JavaScript? Last Updated : 30 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Toggling text is a common functionality in web development. Text toggling means switching between two or more text values when a user performs a specific action, such as clicking a button. This feature can be used in creating expandable/collapsible sections, toggling visibility of text etc.Below are some ways to toggle text with JavaScript:Table of ContentUsing innerTextUsing a CSS ClassUsing innerTextIn this approach, we use innerText method. Here we directly manipulate the text content of an HTML element using JavaScript. This approach involves directly changing the text content of an element using innerText.Example: In the below example we are using the innerText property to toggle the text using javascript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Toggle Text Example 1</title> <style> #toggleButton { padding: 10px 20px; font-size: 16px; cursor: pointer; } </style> </head> <body> <h3>Toggling text using innerText method</h3> <button id="toggleButton">Update</button> <script> let button = document .getElementById('toggleButton'); button .addEventListener('click', function () { if (button.innerText === "Update") { button.innerText = "Edit"; } else { button.innerText = "Update"; } }); </script> </body> </html> Output:OutputUsing a CSS ClassThis approach toggles a CSS class to switch between predefined text values. The text content is defined in the CSS using the content property and the ::before pseudo-element.Example: In this example we will be using CSS Classes to toggle text with JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Toggle Text Using CSS Class</title> <style> #toggleButton { padding: 10px 20px; font-size: 16px; cursor: pointer; border: none; background: lightgray; position: relative; display: inline-block; } .state1::before { content: "Text 1"; } .state2::before { content: "Text 2"; } </style> </head> <body> <h3>Toggling text using CSS class</h3> <button id="toggleButton" class="state1"></button> <script> let button = document .getElementById('toggleButton'); button .addEventListener('click', function () { if (button.classList.contains('state1')) { button.classList.remove('state1'); button.classList.add('state2'); } else { button.classList.remove('state2'); button.classList.add('state1'); } }); </script> </body> </html> Output:Output Comment More infoAdvertise with us Next Article How to Toggle Popup using JavaScript ? G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Toggle Popup using JavaScript ? A popup is a graphical user interface element that appears on top of the current page, often used for notifications, alerts, or additional information. These are the following approaches to toggle a popup using JavaScript: Table of Content Using visibility propertyUsing ClassListUsing visibility pro 3 min read How to toggle a boolean using JavaScript ? A boolean value can be toggled in JavaScript by using two approaches which are discussed below: Table of Content Using the logical NOT operatorUsing the ternary operatorUsing the XOR (^) operatorMethod 1: Using the logical NOT operator The logical NOT operator in Boolean algebra is used to negate an 3 min read How to Toggle Password Visibility in JavaScript ? In this article we will discuss how to toggle password visibility in JavaScript we will also use HTML and CSS in order to create a password visibility toggler for our webpage. Approach: The basic approach towards making password visibility toggler will be quite simple, we will use a button and write 4 min read How to add bootstrap toggle-switch using JavaScript ? Bootstrap toggle-switch is a simple component used for activating one of the two given options. Commonly used as an on/off button. The CSS framework is a library that allows for easier, more standards-compliant web design using the Cascading Style Sheets language. Bootstrap 4 is one such widely used 2 min read How to Toggle an Element Class in JavaScript ? In JavaScript, toggling an element class refers to the process of dynamically adding or removing a CSS class from an HTML element. This allows developers to easily change an element's appearance or behavior in response to user actions, such as clicks or events.These are the following methods for tog 2 min read How to Fix "Toggle JavaScript Works on Second Click, Not First "? If a toggle functionality in JavaScript works only on the second click and not the first, it's often due to issues with the initial state or how event handlers are managing the element's state. Here are some common causes and solutions:Common Causes and Solutions1. Initial State Not Set ProperlyEnsu 3 min read Like