Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
103 lines (102 sloc) 5.46 KB
<?xml version="1.0"?>
<entry type="method" name="append" return="jQuery">
<title>.append()</title>
<signature>
<added>1.0</added>
<argument name="content" type="String, Element, jQuery">
<desc>DOM element, HTML string, or jQuery object to insert at the end of each element in the set of matched elements.</desc>
</argument>
<argument name="content" type="String, Element, Array, jQuery" optional="true">
<desc>One or more additional DOM elements, arrays of elements, HTML strings, or jQuery objects to insert at the end of each element in the set of matched elements.</desc>
</argument>
</signature>
<signature>
<added>1.4</added>
<argument name="function(index, html)" type="Function">
<desc>A function that returns an HTML string, DOM element(s), or jQuery object to insert at the end of each element in the set of matched elements. Receives the index position of the element in the set and the old HTML value of the element as arguments. Within the function, <code>this</code> refers to the current element in the set.
</desc>
</argument>
</signature>
<desc>Insert content, specified by the parameter, to the end of each element in the set of matched elements.</desc>
<longdesc>
<p>The <code>.append()</code> method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the <em>first</em> child, use <a href="http://api.jquery.com/prepend/"><code>.prepend()</code></a>). </p>
<p>The <code>.append()</code> and <code><a href="/appendTo">.appendTo()</a></code> methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With<code> .append()</code>, the selector expression preceding the method is the container into which the content is inserted. With <code>.appendTo()</code>, on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted into the target container.</p>
<p>Consider the following HTML:</p>
<pre><code>&lt;h2&gt;Greetings&lt;/h2&gt;
&lt;div class="container"&gt;
&lt;div class="inner"&gt;Hello&lt;/div&gt;
&lt;div class="inner"&gt;Goodbye&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<p>You can create content and insert it into several elements at once:</p>
<pre><code>$('.inner').append('&lt;p&gt;Test&lt;/p&gt;');
</code></pre>
<p>Each inner <code>&lt;div&gt;</code> element gets this new content:</p>
<pre><code>&lt;h2&gt;Greetings&lt;/h2&gt;
&lt;div class="container"&gt;
&lt;div class="inner"&gt;
Hello
&lt;p&gt;Test&lt;/p&gt;
&lt;/div&gt;
&lt;div class="inner"&gt;
Goodbye
&lt;p&gt;Test&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<p>You can also select an element on the page and insert it into another:</p>
<pre><code>$('.container').append($('h2'));
</code></pre>
<p>If an element selected this way is inserted elsewhere, it will be moved into the target (not cloned):</p>
<pre><code>&lt;div class="container"&gt;
&lt;div class="inner"&gt;Hello&lt;/div&gt;
&lt;div class="inner"&gt;Goodbye&lt;/div&gt;
&lt;h2&gt;Greetings&lt;/h2&gt;
&lt;/div&gt;
</code></pre>
<p>If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first.</p>
<h4 id="additional-arguments">Additional Arguments</h4>
<p>Similar to other content-adding methods such as <code><a href="http://api.jquery.com/prepend/">.prepend()</a></code> and <code><a href="http://api.jquery.com/before/">.before()</a></code>, <code>.append()</code> also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.</p>
<p>For example, the following will insert two new <code>&lt;div&gt;</code>s and an existing <code>&lt;div&gt;</code> as the last three child nodes of the body:</p>
<pre><code>var $newdiv1 = $('&lt;div id="object1"/&gt;'),
newdiv2 = document.createElement('div'),
existingdiv1 = document.getElementById('foo');
$('body').append($newdiv1, [newdiv2, existingdiv1]);
</code></pre>
<p>Since <code>.append()</code> can accept any number of additional arguments, the same result can be achieved by passing in the three <code>&lt;div&gt;</code>s as three separate arguments, like so: <code>$('body').append($newdiv1, newdiv2, existingdiv1)</code>. The type and number of arguments will largely depend on how you collect the elements in your code.</p>
</longdesc>
<example>
<desc>Appends some HTML to all paragraphs.</desc>
<code><![CDATA[
$("p").append("<strong>Hello</strong>");
]]></code>
<css><![CDATA[
p { background:yellow; }
]]></css>
<html><![CDATA[<p>I would like to say: </p>]]></html>
</example>
<example>
<desc>Appends an Element to all paragraphs.</desc>
<code><![CDATA[
$("p").append(document.createTextNode("Hello"));
]]></code>
<css><![CDATA[
p { background:yellow; }
]]></css>
<html><![CDATA[<p>I would like to say: </p>
]]></html>
</example>
<example>
<desc>Appends a jQuery object (similar to an Array of DOM Elements) to all paragraphs.</desc>
<code><![CDATA[
$("p").append( $("strong") );
]]></code>
<css><![CDATA[
p { background:yellow; }
]]></css>
<html><![CDATA[<strong>Hello world!!!</strong><p>I would like to say: </p>]]></html>
</example>
<category slug="manipulation/dom-insertion-inside"/>
<category slug="version/1.0"/>
<category slug="version/1.4"/>
</entry>
Something went wrong with that request. Please try again.