Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active December 3, 2015 19:41
Show Gist options
  • Save khoand0000/8593e9e6c8151eff27f3 to your computer and use it in GitHub Desktop.
Save khoand0000/8593e9e6c8151eff27f3 to your computer and use it in GitHub Desktop.
add element into another element #append #prepend
  • .append() Insert content, specified by the parameter, to the end of each element in the set of matched elements. .appendTo() is same task
<h2>Greetings</h2>
<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>
$( ".inner" ).append( "<p>Test</p>" );
// $( "<p>Test</p>" ).appendTo( ".inner" ); // same
<h2>Greetings</h2>
<div class="container">
  <div class="inner"> <!-- target -->
    Hello
    <p>Test</p> <!-- new element -->
  </div>
  <div class="inner"> <!-- target -->
    Goodbye
    <p>Test</p> <!-- new element -->
  </div>
</div>
  • .prepend() Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. .prependTo() is same task
	$( ".inner" ).prepend( "<p>Test</p>" ); 
	// $( "<p>Test</p>" ).prependTo( ".inner" ); // same
<h2>Greetings</h2>
<div class="container">
  <div class="inner"> <!-- target -->
    Hello
    <p>Test</p> <!-- new element -->
  </div>
  <div class="inner"> <!-- target -->
    Goodbye
    <p>Test</p> <!-- new element -->
  </div>
</div>

Ref:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment