To change div text using jQuery, the simplest way is to use the jQuery text() method:
$("#div").text("Changed Text");
If your div will contain html content, then you should use the jQuery html() method.
$("#div").html("Changed <em>Text</em>");
Let’s say I have the following HTML:
<div id="div">Hello!</div>
If I want to change the text inside the div from “Hello!” to “Good bye!”, I can use the jQuery text() method to do this with the following Javascript code.
$("#div").text("Good bye!");
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery("#div").text("Good bye!");
We can also use the jQuery html() method to change the text of a div.
$("#div").html("Good bye!");
While the text() method is very simple, the html() method gives us the ability to insert html into the element, which gives us more options when styling the content.
Changing the Text of a Div on Click Using jQuery
To change the text of a div on click 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 div below from “Welcome!” to “Good bye!”.
<div id="div1">
<div id="div-to-update">Welcome!</p>
<div id="click-me">Change text</div>
</div>
We can utilize both the jQuery click() method and jQuery text() method to change the text from “Welcome!” to “Good bye!”.
Below is the Javascript code which will allow the user to be able to update the text in the div:
$("#click-me").click(function(){
$("#div-to-update").text("Good bye!");
});
The final code and output for this example of how to change the text of a div using the jQuery text() method is below:
Code Output:
Full Code:
<div id="div1">
<div id="div-to-update">Welcome!</div>
<div id="click-me">Change text</div>
</div>
<script>
$("#click-me").click(function(){
$("#div-to-update").text("Good bye!");
});
</script>
Using the html() Method to Change Text of Div on Click
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 div.
Let’s say we have the following code and we want to add HTML content to our div.
<div id="div">
<div id="div-with-html">We are going to change the html in this div.</div>
<div id="click-me-1">Change text</div>
</div>
If we want to make some text in the div 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 change the text and HTML content inside the div.
$("#click-me").click(function(){
$("#div-with-html").html("The html is now changed with some <strong>bold content</strong>.");
});
The final code and output for this example of how to change the text of a div using the jQuery html() method and Javascript is below:
Code Output:
Full Code:
<div class="html-code-output">
<div id="div">
<div id="div-with-html">We are going to change the html in this div.</div>
<div id="click-me-1">Change text</div>
</div>
</div>
<script>
$("#click-me").click(function(){
$("#div-with-html").html("The html is now changed with some <strong>bold content</strong>.");
});
</script>
Hopefully this article has been useful for you to understand how to use jQuery to change the text of a div.
Leave a Reply