0

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'})); 
2
  • post your html structure to? Commented Apr 8, 2017 at 6:09
  • <li> elements don't have value attributes. That's only for input fields. Commented Apr 8, 2017 at 6:53

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

3 Comments

What is the code your using for appending the ul? Please post your full code in a pastebin.com
Why should he use the html attribute rather than the text attribute if he's just trying to set the text?
@Barmar I got distracted by the assignment of value:1, I suppose, in this context, it doesn't matter. But since he may want to bolden text at some point, I think html is a better option. Nevertheless, edited out.
0

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'
  })
);

3 Comments

@Farzaneh : then get the ul within it
how can i get that ? i just have the data id of the parent

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.