Tuesday, July 4, 2017

Difference between jQuery text() and html() functions.


  • Jquery .text()  can be used in both XML and HTML documents and .html() is only for html documents.
          Exmple:
                    
                   <html>
                  <head>
                         <style>
                               p { color:blue; margin:8px; }
                               b { color:red; }
                       </style>
                      <script src="http://code.jquery.com/jquery-latest.js"></script>
                 </head>
                <body>
                       <p><b>Test</b>Paragraph</p>
                      <p id='second'></p>
                     <p id='last'></p>
                      <p id='final'></p>
              </body>
              </html>
                    
                       var str = $("p:first").text();
                        $("#second").html(str);
                       $("#last").html('<b>my formatting is</b> preserved.');
                       $("#final").text('<b>my formatting is</b> lost and im sanitized');

No comments:

Post a Comment