Home  >  Article  >  Web Front-end  >  JavaScript method to dynamically add LI elements to OL list_javascript skills

JavaScript method to dynamically add LI elements to OL list_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:08:191783browse

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>

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

Statement:
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