• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Change Label Text

Using jQuery to Change Label Text

December 8, 2021 Leave a Comment

To change label text using jQuery, the simplest way is to use the jQuery text() method:

$("label").text("Changed label");

You can also use the jQuery html() method to change the text of a label.

$("label").html("Changed label.");

Let’s say I have the following HTML:

<div id="div1">
  <label id="label-name" for="firstname">First Name:</label>
  <input type="text" id="fname" name="firstname">
</div>

If I want to change the text inside the label from “First Name:” to “Name:”, I can use the jQuery text() method to do this with the following Javascript code.

$("#label-name").text("Name:");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#label-name").text("Name:");

We can also use the jQuery html() method to change the content of a span.

$("#label-name").html("Name:");

While the text() method is very simple, the html() method gives us the ability to insert html into our label, which gives us more options when styling the content.

Changing Label Text Using jQuery with a Click

Many times when creating a web page and the user experience, we want to change the text or content of a form or inputs after an interaction with another element on the web page.

To change the text of a label using jQuery, we can combine the text() method with a click event.

Let’s say I have the following HTML, and I want to give the user the ability to change the text of the span from “First Name” to “Name”.

<div id="div1">
  <div id="click-me">Click here to update the label below.</div>
  <label id="label-name" for="firstname">First Name:</label>>
  <input type="text" id="fname" name="firstname">
</div>

We can utilize both the jQuery click() method and jQuery text() method to change the text in the label from “First Name:” to “Name:”.

Below is the Javascript code which will allow the user to be able to update the text in the span:

$("#click-me").click(function(){
    $("#label-name").text("Name:");
}); 

The final code and output for this example of how to change a label’s text on click using the jQuery text() method and Javascript is below:

Code Output:

Click here to update the label below.


Full Code:

<div class="html-code-output">
<div id="div1">
  <div id="click-me">Click here to update the label below.</div>
  <label id="label-name" for="firstname">First Name:</label>
  <input type="text" id="fname" name="firstname">
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#label-name").text("Name:");
});

</script>

Using the html() Method to Change a Label’s Text

The jQuery html() method is very useful when it comes to manipulating web pages.

We can use the jQuery html() method to change the html and text of a label.

Let’s say we have the following code:

<div id="div1">
  <div id="click-me">Click here to update the label below.</div>
  <label id="label-name" for="firstname">First Name:</label>>
  <input type="text" id="fname" name="firstname">
</div>

If we want to make the text “First” bold, we can do this easily utilizing both the jQuery click() method and jQuery html() method.

Below is the Javascript code which will allow the user to be change the html content and text in the label.

$("#click-me").click(function(){
    $("#label-name").html("<strong>First</strong> Name:");
});

The final code and output for this example of how to change html and text of a label using the jQuery html() method and Javascript is below:

Code Output:

Click here to update the label below.


Full Code:

<div class="html-code-output">
<div id="div1">
  <div id="click-me">Click here to update the label below.</div>
  <label id="label-name" for="firstname">First Name:</label>
  <input type="text" id="fname" name="firstname">
</div>
</div>

<script>

$("#click-me").click(function(){
    $("#label-name").html("<strong>First</strong> Name:");
});

</script>

Hopefully this article has been useful for you to understand how to change text of a label using jQuery.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Remove Background Color
  • 2.  Using jQuery to Remove the Id of a Div
  • 3.  Using jQuery to Convert a String to Lowercase
  • 4.  Using jQuery to Scroll to Top
  • 5.  Changing the Background Image of a div Using jQuery
  • 6.  Examples of Text Animations Using jQuery
  • 7.  Using jQuery to Get Value of Select On Change
  • 8.  Get nth Child with jQuery
  • 9.  Using jQuery to Set the Id of a Div
  • 10.  Using jQuery to Get the Value of a Radio Button

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy