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

$("#div1 span").text("Changed Text");

If your span will contain html content, then you should use the jQuery html() method.

$("#div1 span").html("Changed Text");

Let’s say I have the following HTML:

Today is November 3rd.

If I want to change the text inside the span from “November” to “December”, I can use the jQuery text() method to do this with the following Javascript code.

$("#div1 span").text("December");

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

jQuery("#div1 span").text("December");

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

$("#div1 span").html("December");

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

Changing the Text in a Span Using jQuery with a Click

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

To change the text of a span 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 “November” to “December”.

Click here to update span.

Today is November 3rd.

We can utilize both the jQuery click() method and jQuery text() method to change the text from “November” to “December”.

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

$("#click-me").click(function(){
    $("#div1 span").text("December");
}); 

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

Code Output:

Click here to update span.

Today is November 3rd.

Full Code:

Click here to update span.

Today is November 3rd.

<script> $("#click-me").click(function(){ $("#div1 span").text("December"); }); </script>

Using the html() Method to Change Text in Span

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 span.

Let’s say we have the following code and we want to give the user the ability to add html content to the span.

Click here to update span.

We are going to add html to this span with content.

If we want to make the text “content” 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 content in the span.

$("#click-me").click(function(){
    $("#div1 span").html("span with content");
});

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

Code Output:

Click here to update span.

We are going to add html to this span with content.

Full Code:

Click here to update span.

We are going to add html to this span with content.

<script> $("#click-me").click(function(){ $("#div1 span").html("span with content"); }); </script>

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

Categorized in:

jQuery,

Last Update: February 26, 2024