Remove First li from ul JavaScript

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

removeChild() method

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

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

firstElementChild property

firstElementChild property returns the first child of the HTML element.

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

firstElementChild property is a read only property.

HTML Code

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>
<html>
<head>
<title>Remove First li from ul JavaScript</title>
</head>
<body>

<ul id="list">
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
<button onclick="deleteFirstLi()">Delete First li</button>    
    
<script src="script.js"></script>    
</body>
</html>

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 first child of ul tag which is first list item.

firstElementChild property is used to target or select the first list item.

<script>
function deleteFirstLi()
{
    var ul = document.getElementById('list');
    ul.removeChild(ul.firstElementChild);
}
</script>

Demo

Video Tutorial

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

How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using 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