0

ı know the topic is not specific. let me tell you.

ı have a text box and button. when you enter something and click button its creating div and appending it to another div. but the problem is its must append it to top of the page. its going to be second div.

jquery code:

$('#send').click(function() {
   var not=$('#not').val();

   $.ajax({
    type:"POST",            
    url: "some.php?islem=not",
    data:{not:not},
    success: function(result){
        $('#comment_box').append('<div class="comment">'+not+'</div>'); 
    }   
   });
});

html code:

<textarea style="width: 794px; height: 48px;" id="not"></textarea>
<input type="button" value="Yolla" style="margin-top:35px;" id="send"/>

<div id="comment_box">
    <div class="comment">comment-1</div>
    <div class="comment">there is comment in here-2</div>
    <div class="comment">there is comment in here-3</div>
    <div class="comment">there is comment in here-4</div>
    <div class="comment">there is comment in here-5</div>
</div>

when ı add something the new div is shown after comment-1, ı want it to be first. because its the latest comment and it must be first, at the top of the page.

sory for bad english.

5 Answers 5

3

try .prepend() http://api.jquery.com/prepend/

1

I'd suggest more or less the same thing, but with a slight twist on your originally-posted code:

$('<div />', {'class' : 'comment'}).prependTo('#comment_box');

The element-creation specifies the element to be created, and as the second parameter takes an object defining the attribute/properties with which that element should be created.

This makes it a little easier to maintain code in the long run (in large projects), though admittedly this is, more or less, a purely personal opinion with no metrics with which I can offer support for this approach.

Edited in response to question from OP:

where should ı insert text? ı want to add div with data.

To add text to the element, there's two approaches:

$('<div />', {'class' : 'comment', 'text' : not}).prependTo('#comment_box');

JS Fiddle proof-of-concept.

Or:

$('<div />', {'class' : 'comment'}).text(not).prependTo('#comment_box');

JS Fiddle proof-of-concept.

Edited in response to question from OP:

can ı ask 1 last question. ı want to add after not but its not working

For this, you need to get a reference to the div you inserted and then, simply insert the br element after that div:

var newDiv = $('<div />', {'class' : 'comment'}).text(not).prependTo('#comment_box');
$('<br />').insertAfter(newDiv);

If you want the br within the div you've just created, then simply use append():

$('<div />', {'class' : 'comment', 'text' : not}).append('<br />').prependTo('#comment_box');

Or:

$('<div />', {'class' : 'comment'}).text(not).append('<br />').prependTo('#comment_box');

References:

4
  • where should ı insert text? ı want to add div with data.
    – bayburt
    Aug 27, 2012 at 14:30
  • can ı ask 1 last question. ı want to add </br> after not but its not working. this: $('<div />', {'class' : 'comment', 'text' : not+'</br> sended by someone'}).prependTo('#comment_box'); </br> is not working now.
    – bayburt
    Aug 27, 2012 at 15:00
  • I think the edit covers both possibilities of what you were asking for? Aug 27, 2012 at 15:04
  • yes its. not after. basicly message br some text. thank you for your great helps david.
    – bayburt
    Aug 27, 2012 at 15:12
1

Use http://api.jquery.com/prepend/ perpend instead of append

1

Instead of .append() use .prepend().

1

Use 'before' rather than append:

$('#comment_box').before('<div class="comment">'+not+'</div>');

*Loving the I in the turkish keyboard hehe

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.