jQuery text()
Syntax:
To return text content:
$(selector).text()
To set text content:
$(selector).text(content)
To set text content using a function:
$(selector).text(function(index,currentcontent))
jQuery html()
Syntax:
To return text content:
$(selector).html()
To set text content:
$(selector).html(content)
To set text content using a function:
$(selector).html(function (index, currentcontent))
The difference between jquery text() and jquery html():
- When set content method is used in either of them, it overwrites the content of the all matched elements.
- When return content method is used in jquery html() , it returns the content of the first matched element and when used in jquery text(), it returns the content of all matched element.
- Both XML and HTML document can make use of jquery text() method while jquery html() method can be used only in html document.
- The jquery html() method sets or returns the innerHtml (text + HTML markup) while the jquery text() method sets or returns html content without HTML markup.
Example:
<html>
<head>
<title>Test Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#div1").html('<a href="example.html">Link1</a><b>hello</b>');
$("#div2").text('<a href="example.html">Link2</a><b>hello</b>');
});
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
Output:
Link11hello
<a href="example.html">Link2</a><b>hello</b>
0 Comment(s)