-2
<div id="everything">
  <ol>
    <li>Here is thing one.</li>
    <li>Here is thing two.</li>
    <li>Here is thing three.</li>
  </ol>

I want to append an additional li to the ol list, how do I do that using append()?

edit: I figured out my problem was that I wasn't wrapping my append() with tags.

I want to append a new list item that says "Here is thing four." How would I do that using append()?

jb151
  • 11
  • 2

2 Answers2

2

The code below creates an item element (li) and append it to an element with the selector "everything".

$('<li>Your text</li>').appendTo('#everything');

The advantage of using this method is that you can append the item element to an unordered HTML list (ul), or an ordered HTML list (ol).

Here is the link to the documentation for the appendTo method.

Description: Insert every element in the set of matched elements to the end of the target.

Note:

Please notice that I used the selector that you provided, #everything, and didn't use the string ol instead. The reason is that if I would do that, it would append the item to any ordered list you have in your page.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
acarlstein
  • 1,799
  • 2
  • 13
  • 21
0

In its basic form, the .append() function will help you (as would a quick Google search).

$( "ol" ).append( "<li>Here is thing four.</li>" );
Adam
  • 1,149
  • 2
  • 14
  • 21
  • I would only modify this code to use the selector provided, #everything, else any ordered list in the page will have this element append. In other words, if the user has more than one ordered list on the page, the same item would show up on those lists. – acarlstein Jul 26 '18 at 03:11