How to Add <li> in an Existing <ul> using jQuery
Topic: JavaScript / jQueryPrev|Next
Answer: Use the jQuery append()
Method
You can simply use the jQuery append()
method to add <li>
elements in an existing <ul>
element.
The following example will add a <li>
element at the end of an <ul>
on click of the button.
Example
Try this code »<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Add LI to an Existing UL</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#menu").append('<li><a href="#">New list item</a></li>');
});
});
</script>
</head>
<body>
<ul id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
</ul>
<button type="button">Add New LI Element</button>
</body>
</html>
Related FAQ
Here are some more FAQ related to this topic: