jQuery HTML Element How to - Add escaped text to the paragraph








Question

We would like to know how to add escaped text to the paragraph.

Answer


<!--from  w  w  w.ja  va2s.c o m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").text("<b>Some</b> new text.");
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
      <p class="selected highlight">Hello first</p>
      <p class="">Hello</p>
  </body>
</html>

The code above is rendered as follows: