ı 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.
I’d suggest more or less the same thing, but with a slight twist on your originally-posted code:
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:
To add text to the element, there’s two approaches:
JS Fiddle proof-of-concept.
Or:
JS Fiddle proof-of-concept.
Edited in response to question from OP:
For this, you need to get a reference to the
divyou inserted and then, simply insert thebrelement after thatdiv:If you want the
brwithin thedivyou’ve just created, then simply useappend():Or:
References:
append().jQuery(html, props), it’s the last part of that section.prependTo().text().