jQuery Prepend

Prepending an element to a container

Solution 1:


Solution 2:


Both solutions are prepending the element #child (adding at the beginning) to the element #parent.


<div id="parent">
  <span>other content</span>
<div id="child">



<div id="parent">
  <div id="child">

  <span>other content</span>

Prepend method

prepend() - Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.

1. prepend( content [, content ] )

// with html string
// or you can use jQuery object
// or you can use comma seperated multiple elements to prepend

2. prepend(function)

JQuery version: 1.4 onwards you can use callback function as the argument. Where you can get arguments as index position of the element in the set and the old HTML value of the element. Within the function, this refers to the current element in the set.

     // return the value to be prepend
     return  '<span>child</span>';