Remove Last li from ul JavaScript

In this tutorial we will learn How To Remove Last li from ul with JavaScript. HTML DOM Element removeChild() method and lastElementChild property can be used to delete the last li from ul tag.

removeChild() method

removeChild() method removes the child of the HTML element.

In this example it is used to remove the last li tag of ul tag.

lastElementChild property

lastElementChild property returns the last child of the HTML element.

In this example it is used to get the last li tag.

lastElementChild property is a read only property.


HTML Code is given below, in this code we have a ul tag with three list items and a button tag with onclick event.

<!DOCTYPE html>
<title>Remove Last li from ul JavaScript</title>

<ul id="list">
<button onclick="deleteLastLi()">Delete Last li</button>    
<script src="script.js"></script>    

JavaScript Code

Take a look at the JavaScript code, in this code getElementById() method is used to select the ul tag. While removeChild() method removes the last child of ul tag which is last list item. lastElementChild property is used to target last list item.

function deleteLastLi()
    var ul = document.getElementById('list');


Video Tutorial

Watch video tutorial on How To Remove Last li from ul tag in JavaScript.

How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript Change id of Element with JavaScript JavaScript Change li Text Color JavaScript Change li Text JavaScript Remove id from Element Check If id Exists in JavaScript How To Delete Table Row by id in JavaScript Update Textarea value in JavaScript Change Variable Value in JavaScript Uncheck Radio Button Dynamically JavaScript Check Radio Button Dynamically JavaScript JavaScript Check if Radio Button is Checked or Not Change h1 Tag Text with JavaScript