Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery How to create element in created element

There is a code I'm working on - https://jsfiddle.net/md_bender/pj6a1akz/3/. I need to create new elements and append them to the existing element ul. With creating elements and appending everything is OK. But I don't know how to create child element in the created element. I need to create

<img src="path"/>

In the div I have created earlier in script. It must be like that

<div class="img-w js-popup">
    <img src="path"/>
</div>
<a href="delete.php">Delete</a>

But only I can do is create as next sibling to the div

<div class="img-w js-popup"></div>
<img src="path"/>
<a href="delete.php">Delete</a>

Who can help me and solve my problem?

like image 422
mdBender Avatar asked Mar 26 '26 01:03

mdBender


1 Answers

You can append img to div before you append that div to li. You can also write the same code like this Fiddle

$('<li/>')
  .append($('<div/>', {
    'class': 'img-w js-popup',
  }).append($('<img/>', {
    'src': '//www.gravatar.com/avatar/9142888a2ae6160f2faec90134eeafa3/?default=&s=80'
  })))
  .append($('<a/>', {
    href: 'delete.php',
    text: 'Delete'
  }))
  .appendTo('ul');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <div>
      <img src="//www.gravatar.com/avatar/9142888a2ae6160f2faec90134eeafa3/?default=&s=80" alt="" />
    </div>
    <a href="delete.php">Delete</a>
  </li>
</ul>
like image 133
Nenad Vracar Avatar answered Mar 27 '26 14:03

Nenad Vracar



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!