jQuery

[jQuery] jQuery/Method/.append() - Method to add content to the end of the selected element's content

OOQ 2022. 8. 30. 10:05
728x90
SMALL

#jQuery

#append()

 

.append()

.append() appends the content to the end of the content of the selected element.


grammar

 

.append( content [, content ] )


for example

<p>Lorem Ipsum Doll</p>



$( 'p' ).append( '123' );


Speaking of

<p>Lorem Ipsum Doll 123</p>


is output to

 

example1

Add Dolor to the end of an unordered list.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      body {
        line-height: 2;
        font-family: sans-serif;
        font-size: 20px;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'ul' ).append( '<li>Dolor</li>' );
      } );
    </script>
  </head>
  <body>
    <ul>
      <li>Lorem</li>
      <li>Ipsum</li>
    </ul>
  </body>
</html>

 

example2

Move the strong element to the end of the contents of the p element.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      body {
        line-height: 2;
        font-family: sans-serif;
        font-size: 20px;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'p' ).append( $( 'strong' ) );
      } );
    </script>
  </head>
  <body>
    <p>abc</p>
    <strong>XYZ</strong>
  </body>
</html>

 

Use .

  • prepend() to add content before the content of the selected element.
  • jQuery/Method/.appendTo()
728x90
LIST