How to change the text of a label using JavaScript ?
Given an HTML document and the task is to change the text of a label using JavaScript.
What is a label ?
The <label>tag is used to provide a usability improvement for mouse users i.e, if a user clicks on the text within the <label> element, it toggles the control.
Approach:
- Create a label element and assign an id to that element.
- Define a button that is used to call a function. It acts as a switch to change the text in the label element.
- Define a javaScript function, that will update the label text.
- Use the innerHTML property to change the text inside the label. The innerHTML property sets or returns the HTML content of an element.
Example 1: This example implements the above approach.
<!DOCTYPE html> <html> <head> <title> How to change the text of a label using JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h4> Click on the button to change the text of a label </h4> <label id = "GFG"> Welcome to GeeksforGeeks </label> <br> <button onclick="myGeeks()"> Click Here! </button> <script> function myGeeks() { document.getElementById('GFG').innerHTML = 'A computer science portal for geeks'; } </script> </body> </html> |
Output:
- Before Click on the Button:

- After Click on the Button:

Example 2: This example changes the text of a label using JavaScript.
<!DOCTYPE html> <html> <head> <title> How to change the text of a label using JavaScript ? </title> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h4> Click on the button to change the text of a label </h4> <label id = "GFG"> Welcome to GeeksforGeeks </label> <br> <button onclick="myGeeks()"> Click Here! </button> <script> function myGeeks() { var x = document.getElementById("GFG"); if (x.innerHTML === "Welcome to GeeksforGeeks") { x.innerHTML = "A computer science portal for geeks"; } else { x.innerHTML = "Welcome to GeeksforGeeks"; } } </script> </body> </html> |
Output:
- Before Click on the Button:

- After Click on the Button:

Recommended Posts:
- JavaScript | Change the text of a span element
- How to change text color depending on background color using JavaScript?
- How to select all text in HTML text input when clicked using JavaScript?
- How to Change the Text of a Button using jQuery?
- JQuery | Change the text of a span element
- Change an element class JavaScript
- How to change the Content of a <textarea> using JavaScript ?
- How to change the shape of a textarea using JavaScript?
- How to change the src attribute of an img element in JavaScript / jQuery ?
- How to dynamically change the title of web page using JavaScript ?
- How to process each letter of text using JavaScript ?
- JavaScript | Get the text of a span element
- How to get the Highlighted/Selected text in JavaScript?
- Calculate the width of the text in JavaScript
- How to get the value of text input field using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


