views:

1178

answers:

4

How can I add multiple paragraph tag, newly tag on top within div container.

<div id="pcontainer">
  <p>recently added on top every time on click event recently added paragarph on top</p>
  <p>added before recent</p>
</div>

I am using append but every time I click button it add to bottom I need it to added on top of all paragraph please help.

+2  A: 

Try using prepend instead of append.

tvanfosson
+5  A: 

You may use prepend to add the paragraph at the top of the container:

// HTML: <div><p>Lorem ipsum</p></div>
$('div').prepend('<p>Bla bla bla');

Update: Regarding your comment about how to fade in the paragraph - use fadeIn:

$("#pcontainer").prepend($('<p>This paragraph was added by jQuery.</p>').fadeIn('slow'));

A working demo: http://jsbin.com/uneso

moff
you are alos right Moff both works, how i can add paragraph with fadein affect ?
Yasir
Moff thanks, this just hiding the newly added p i need it to show with fade in affect how ? thanks in advance
Yasir
Okay, I've corrected the post now :)
moff
Thanks Moff great its working fine.
Yasir
A: 

Specific to your HTML, it would be:

$("#pcontainer").prepend('<p>here's a new paragraph!</p>');
Brandon Montgomery
You're right. Just don't forget to escape your apostrophe there :)
moff
how i can add fade in affect with this ?
Yasir
You'd have to hide the paragraph first, then call the fadeIn function. So initially, it would be <p class="pHidden" style="display: none;">lkjsdlfkjs</p>, then you'd call $(".pHidden").fadeIn();
Brandon Montgomery
A: 

An example for non jQuery-users:

document.getElementById('pcontainer').innerHTML = '<p>new paragraph</p>' + document.getElementById('pcontainer').innerHTML;

maybe not as short and nice though :)