JQuery tutorial: add text


JQuery

Add text

You can add text to an HTML element with JQuery code by using text() function.

Example:
<html>
<head><title>Add text to a div tag</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){

$("div#firstdiv").text("This text in the div tag is written dynamically by JQuery.");
});

</script>

</head>
<body>
<div id="firstdiv">

<div>
</body>
</html>
 
If you want to keep the old text and append the new text, you can use append() function.
Example:
<html>
<head><title>Append text to the div tag</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){

$("div#firstdiv").append("This new text is appended dynamically by JQuery.");
});

</script>

</head>
<body>
<div id="firstdiv">
This is the old text of div tag.
<div>
</body>
</html>



Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.