Home
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script> <script> $(document).ready(function () { $('#btnDiv').click(function(){ alert($('#myDiv').text()); }); $('#btnP').click(function(){ alert($('p').text()); }); //removes all the content from #emptyDiv and inserts "This is some text." to #emptyDiv $('#emptyDiv').text('This is some text.'); }); </script> <style> .yellowDiv{ background-color:yellow; margin:5px 0 0 0; } </style> </head> <body> <h1>Demo: jQuery text() method</h1> <button id="btnDiv">Get div text</button> <button id="btnP">Get p text</button> <div id="myDiv" class="yellowDiv"> <p style="font-size:16px;font-weight:bold"> This is paragraph. </p> </div> <div id="emptyDiv"> </div> </body> </html>
Result: