jQuery Tutorial Tutorials - jQuery .prependTo() method

jQuery .prependTo() method

The .prependTo(target) method in jQuery is used to insert every matched element to the beginning of target.

jQuery .prependTo() method Syntax

.prependTo(target)
Parameter Type Description
target htmlString,element(s),text,jQuery object DOM target to be inserted to.

.prependTo(target)

example

Insert text at the beginning of all paragraphs
HTML

<p>Learn JavaScript</p>
<p>Learn jQuery</p>
<p>Learn Python</p>

jQuery

$("<strong> At Lautturi.com </strong>").prependTo("p");

Result

<p><strong> on Lautturi.com </strong>Learn JavaScript</p>
<p><strong> on Lautturi.com </strong>Learn jQuery</p>
<p><strong> on Lautturi.com </strong>Learn Python</p>

Try now

example

Move the dom element (not clone)
HTML

<h2>tutorial</h2>
<div id="container">
    <p>Learn JavaScript</p>
    <p>Learn jQuery</p>
    <p>Learn Python</p>
</div>

jQuery

$("h2").prependTo($('#container'));

Result

<div id="container">    
    <h2>tutorial</h2>
    <p>Learn JavaScript</p>
    <p>Learn jQuery</p>
    <p>Learn Python</p>
</div>

Try now

Date:2019-08-25 09:56:17 From:www.Lautturi.com author:Lautturi