Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending to specific element append text only

I have prepended one div inside some div with many others:

<div class="content">
<div>sss</div>
<div>aaa</div>
<div>bbb</div>
</div>

I added <div>ddd</div> before sss with:

$('.content').prepend('<div></div>');

And when I want to append some new element to new prepended div it add's it as text:

$('.content>div')[0].append('<p>ddd</p>');

If I remove [0] it works but it appends to all divs, I need that [0] to find first div.


1 Answers

To get the first element (that is still a jquery object) you can use .first():

$('.content>div').first().append($('<p>ddd</p>'));

Note that I also wrapped the <p> with $(...) to make it a valid html element (and not text).

like image 141
Dekel Avatar answered Jun 07 '26 23:06

Dekel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!