Farzaneh
Farzaneh

Reputation: 111

appending with data attribute in jQuery

How can I append "LI" tag to "UL" tag of a DIV with this data-id?

Where should I add UL ?

 $('[data-id="car-model"]').append($('<li></li>', {value:1, text:'One'})); 

Upvotes: 0

Views: 2224

Answers (2)

Neil
Neil

Reputation: 14321

You're really close with your code you just have to append an li and then add the ul to your selector.

$('[data-id="car-model"] ul').append($('<li>', {
  html: "Really new item"
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-id="car-model">
  <ul>
    <li>Test1</li>
    <li>Test2</li>
  </ul>
</div>

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115282

Get the ul within the element by updating the selector([data-id="car-model"] ul) and append li to it.

$('[data-id="car-model"] ul').append($('<li></li>', {
    text: 'One'
  })
);

Upvotes: 0

Related Questions