How to insert element as a first child?
Asked Answered
N

8

121

I want to add a div as a first element using jquery on each click of a button

<div id='parent-div'>
    <!--insert element as a first child here ...-->

    <div class='child-div'>some text</div>
    <div class='child-div'>some text</div>
    <div class='child-div'>some text</div>

</div> 
Nissie answered 24/12, 2010 at 18:59 Comment(1)
The answers to this question also work with an empty list of child-div elements. Great!Upcast
P
193

Try the $.prepend() function.

Usage

$("#parent-div").prepend("<div class='child-div'>some text</div>");

Demo

var i = 0;
$(document).ready(function () {
    $('.add').on('click', function (event) {
        var html = "<div class='child-div'>some text " + i++ + "</div>";
        $("#parent-div").prepend(html);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="parent-div">
    <div>Hello World</div>
</div>
<input type="button" value="add" class="add" />
Planoconcave answered 24/12, 2010 at 19:2 Comment(2)
the problem with this solution is that it inserts as the first child and not before the list children, if the parent container contains different children elements and one wants to insert before a specific group of child nodes, this will not work.Apodaca
At first, it was not apparent to me that this solution also works if there are zero child-div elements. prepend() will insert into the empty list and create the first child-div element. Of course, append() also works, but the list is created in another order.Upcast
R
26

Extending on what @vabhatia said, this is what you want in native JavaScript (without JQuery).

ParentNode.insertBefore(<your element>, ParentNode.firstChild);

Restraint answered 16/6, 2015 at 8:48 Comment(0)
M
7

Use: $("<p>Test</p>").prependTo(".inner"); Check out the .prepend documentation on jquery.com

Missilery answered 12/2, 2014 at 9:47 Comment(1)
this will insert multiple nodes, one before each child.Apodaca
M
6
parentNode.insertBefore(newChild, refChild)

Inserts the node newChild as a child of parentNode before the existing child node refChild. (Returns newChild.)

If refChild is null, newChild is added at the end of the list of children. Equivalently, and more readably, use parentNode.appendChild(newChild).

Melt answered 14/5, 2014 at 4:36 Comment(1)
literally copy and pasted from another post. perhaps give reference to the specific question and how it relates?Ferree
S
5
parentElement.prepend(newFirstChild);

This is a new addition in (likely) ES7. It is now vanilla JS, probably due to the popularity in jQuery. It is currently available in Chrome, FF, and Opera. Transpilers should be able to handle it until it becomes available everywhere.

P.S. You can directly prepend strings

parentElement.prepend('This text!');

Links: developer.mozilla.org - Polyfill

Silverman answered 8/5, 2017 at 0:26 Comment(0)
G
1
$('.parent-div').children(':first').before("<div class='child-div'>some text</div>");
Gerund answered 14/3, 2014 at 9:50 Comment(1)
this will insert before each child, ie you will end up inserting multiple nodes.Apodaca
G
0

Required here

<div class="outer">Outer Text <div class="inner"> Inner Text</div> </div>

added by

$(document).ready(function(){ $('.inner').prepend('<div class="middle">New Text Middle</div>'); });

Gambrill answered 5/8, 2015 at 7:57 Comment(0)
I
-1

$(".child-div div:first").before("Your div code or some text");

Infectious answered 11/3, 2014 at 11:51 Comment(1)
there are not children nodes below child-divApodaca

© 2022 - 2024 — McMap. All rights reserved.