This post will discuss how to add an item to an ordered or unordered list in HTML in JavaScript and jQuery.

1. Using jQuery

With jQuery, you can create a new <li> element and add it to an existing <ol> or <ul> element using the .append() or the .prepend() method.

The .append() method inserts the specified content at the end of the matched elements, while the .prepend() method inserts it at the beginning.

jQuery


HTML



Edit in JSFiddle

 
The code can be shortened to:

jQuery


HTML



Edit in JSFiddle

 
Instead of .append() method, you can also use the .after() or .before() method to insert the specified content after or before a specified list element, respectively.

jQuery


HTML



Edit in JSFiddle

 
You can also use the .appendTo()/.prependTo() method, which is similar to the .append()/.prepend() method but has different syntax.

jQuery


HTML



Edit in JSFiddle

2. Using JavaScript

In pure JavaScript, you can create a <li> element using the createElement() method, and then append it to the list with Node.appendChild() method.

JS


HTML



Edit in JSFiddle

That’s all about adding an item to an HTML list in JavaScript and jQuery.