Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 10, 2023 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/aa55dc06009996c9e190c411dbd0b82f to your computer and use it in GitHub Desktop.
Save code-boxx/aa55dc06009996c9e190c411dbd0b82f to your computer and use it in GitHub Desktop.
Javascript Add Remove HTML List Items

JAVASCRIPT ADD REMOVE HTML LIST ITEMS

https://code-boxx.com/add-remove-list-items-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Add Remove List Items</title>
</head>
<body>
<!-- (A) HTML LIST -->
<ol id="demoA">
<li>First</li>
<li>Second</li>
</ol>
<!-- (B) JAVASCRIPT ADD -->
<script>
function addItem () {
// (B1) CREATE NEW LIST ITEM
var item = document.createElement("li");
item.innerHTML = "New";
// (B2) APPEND TO LIST
document.getElementById("demoA").appendChild(item);
}
</script>
<input type="button" value="Add Item" onclick="addItem()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Add Remove List Items</title>
</head>
<body>
<!-- (A) HTML LIST -->
<ol id="demoB">
<li>First</li>
<li>Second</li>
</ol>
<!-- (B) JAVASCRIPT ADD -->
<script>
function addItemPos (top) {
// (B1) CREATE NEW LIST ITEM
var item = document.createElement("li");
item.innerHTML = "New";
// (B2) APPEND ITEM TO TOP
if (top) {
document.getElementById("demoB").insertAdjacentElement("afterbegin", item);
}
// (B3) IN THE MIDDLE
else {
var allItems = document.querySelectorAll("#demoB li");
var allLength = allItems.length;
var middle = Math.ceil(allLength/2) - 1;
if (middle<0) { middle = 0; }
allItems[middle].insertAdjacentElement("afterend", item);
}
}
</script>
<input type="button" value="Add Top" onclick="addItemPos(1)">
<input type="button" value="Add Middle" onclick="addItemPos()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Add Remove List Items</title>
</head>
<body>
<!-- (A) HTML LIST -->
<ol id="demoC">
<li>First</li>
<li>Second</li>
</ol>
<!-- (B) JAVASCRIPT ADD -->
<script>
function addItemHTML () {
document.getElementById("demoC").innerHTML += "<li>New</li>";
}
</script>
<input type="button" value="Add Item" onclick="addItemHTML()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Add Remove List Items</title>
</head>
<body>
<!-- (A) HTML LIST -->
<ol id="demoD">
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Forth</li>
</ol>
<!-- (B) JAVASCRIPT REMOVE -->
<script>
function delItem (first) {
// (B1) GET LIST + ALL ITEMS
var list = document.getElementById("demoD");
var allItems = document.querySelectorAll("#demoD li");
// (B2) REMOVE FIRST ITEM
if (first) {
list.removeChild(allItems[0]);
}
// (B3) REMOVE LAST ITEM
else {
var last = allItems.length - 1;
list.removeChild(allItems[last]);
}
}
</script>
<input type="button" value="Remove First" onclick="delItem(1)">
<input type="button" value="Remove Last" onclick="delItem()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment