Home > Web Front-end > JS Tutorial > body text

JavaScript method to dynamically add LI elements to OL list_javascript skills

WBOY
Release: 2016-05-16 16:08:19
Original
1784 people have browsed it

The example in this article describes the method of dynamically adding LI elements to the OL list using JavaScript. Share it with everyone for your reference. The specific analysis is as follows:

JavaScript method to dynamically add LI elements to the OL list. The following JS code will dynamically add an LI element to the OL list every time the button is clicked

<script type="text/javascript">
function addItem() {
 var myitem = document.getElementById("ItemToAdd").value;
 var mylistItems = document.getElementById("mylist");
 var newP = document.createElement("li");
 var textNode = document.createTextNode(myitem);
 newP.appendChild(textNode);
 document.getElementById("mylist").appendChild(newP);
 return false;
}
</script>
<form onsubmit="return addItem()" action="#">
<span>Grocery Items:</span>
<input type="text" id="ItemToAdd" value="Milk" />
<input type="button" value="Add" onclick="addItem()" />
</form>
<span>Grocery List:</span>
<ol id="mylist"></ol>
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template