How to get the value of div content using jQuery?



To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.

Example

You can try to run the following code to get the value of div content using jQuery:

Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
   $("#button1").click(function() {
     var res = $('#demo').text();
     alert(res);
   });
});
</script>
</head>
<body>

<div id="demo"> This is <b>demo</b> text.
</div>
<button id="button1">Get</button>

</body>
</html>

Advertisements