Thomas
Thomas

Reputation: 34188

appending one div in another div before last div jquery

suppose i have div like

<div id='main'>
  <div id='child'>my static content</div>
</div>

$('#main').append("<div class='mycontent'>I'm new box by append</div>");
or
$('#main').appendTo("<div class='mycontent'>I'm new box by append</div>");

i know i can use append() and appendTo method to insert anything into main div. but i need to know that how can i insert content into main div before child div. content will be pushed into main div before child div. please help me with concept.

thanks

Upvotes: 1

Views: 4622

Answers (3)

Vytautas
Vytautas

Reputation: 3539

try this:

$('#child').before("<div class='mycontent'>I'm new box by append</div>");

also maybe will be useful:

$('#child').after("<div class='mycontent'>I'm new box by append</div>");

Upvotes: 0

Gavriel
Gavriel

Reputation: 19237

http://api.jquery.com/before/

if you want it before the last child div:

$("#main > div:last-child").before("<div class='mycontent'>I'm new box by append</div>");

or if you want it before a specific div:

$("#child").before("<div class='mycontent'>I'm new box by append</div>");

Upvotes: 2

Ram
Ram

Reputation: 144689

this way:

$('#main #child').before('<p>Something</p>');

Upvotes: 1

Related Questions