jQuery DOM insertion - html method

html method

html() method is used to get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched HTML element.

Getting HTML Element Contents

.html() // to get the html contents.

Getting HTML Contents Example

<html>
   <head>
      <title>The jQuery Example</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>		
      <script type = "text/javascript">
	
        $(document).ready(function(){
         alert( $('.codingpointer').html());
        });
      </script>		
   </head>	
   <body>
   <p>Test Section</p>
   <div>
     <div class="codingpointer">Hello</div>
     <div class="codingpointer">Goodbye</div>
  </div>
   </body>
</html>
Output:
jquery html method output

Setting HTML Element Contents

.html(html string) // to set the html contents.

Setting HTML Contents Example

here setting the HTML element content using html string.
<html>
   <head>
      <title>The jQuery Example</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>		
      <script type = "text/javascript">
	
        $(document).ready(function(){
         $("p").html("Welcome to jQuery!");
        });
      </script>		
   </head>	
   <body>
   <p>Test Section</p>
   <div>
     <div class="codingpointer">Hello</div>
     <div class="codingpointer">Goodbye</div>
  </div>
   </body>
</html>
Output:

Welcome to jQuery!

Hello
Goodbye
here setting html element content using another html element contents.
<html>
   <head>
      <title>The jQuery Example</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>		
      <script type = "text/javascript">
	
        $(document).ready(function(){
         $("p").html( $('.codingpointer').html());
        });
      </script>		
   </head>	
   <body>
   <p>Test Section</p>
   <div>
     <div class="codingpointer">Hello</div>
     <div class="codingpointer">Goodbye</div>
  </div>
   </body>
</html>
Output:

Hello

Hello
Goodbye

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^