jQuery

How to get the text content of an element with jQuery

In this tutorial, we are going to see how to get the text content of an element with jQuery. You can simply use the method text() of jQuery to get all the content of the text contained in the element. The text() method also returns the textual content of the child elements.
 

How to get the text content of an element with jQuery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Get the text content of an element</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("button").click(function(){
					var divContent = $('#box').text();
					alert(divContent);
				});
			});
		</script>
	</head>
	<body>
		<div id="box">
			<p>Lorem ipsum elit sit ut, consectetur adipiscing dolor.</p> 
		</div>
		<button type="button">Get the text</button>
	</body>
</html>
Result

Lorem ipsum elit sit ut, consectetur adipiscing dolor.

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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