Create ul and li elements in javascript.
Great then. Let's create a simple function that takes an array and prints our an ordered listview/list inside a div tag.
Step 1: Let's say you have an div with "contentSectionID" id.<div id="contentSectionID"></div>
Step 2: We then create our javascript function that returns a list component and takes in an array:
function createList(spacecrafts){
var listView=document.createElement('ol');
for(var i=0;i<spacecrafts.length;i++)
{
var listViewItem=document.createElement('li');
listViewItem.appendChild(document.createTextNode(spacecrafts[i]));
listView.appendChild(listViewItem);
}
return listView;
}
Step 3: Finally we select our div and create a listview in it:
document.getElementById("contentSectionID").appendChild(createList(myArr));
Here is my working code :
<!DOCTYPE html>
<html>
<head>
<style>
ul#proList{list-style-position: inside}
li.item{list-style:none; padding:5px;}
</style>
</head>
<body>
<div id="renderList"></div>
</body>
<script>
(function(){
var ul = document.createElement('ul');
ul.setAttribute('id','proList');
productList = ['Electronics Watch','House wear Items','Kids wear','Women Fashion'];
document.getElementById('renderList').appendChild(ul);
productList.forEach(renderProductList);
function renderProductList(element, index, arr) {
var li = document.createElement('li');
li.setAttribute('class','item');
ul.appendChild(li);
li.innerHTML=li.innerHTML + element;
}
})();
</script>
</html>
working jsfiddle example here
Try out below code snippet:
var list = [];
var text;
function update() {
console.log(list);
for (var i = 0; i < list.length; i++) {
console.log( i );
var letters;
var ul = document.getElementById("list");
var li = document.createElement("li");
li.appendChild(document.createTextNode(list[i]));
ul.appendChild(li);
letters += "<li>" + list[i] + "</li>";
}
document.getElementById("list").innerHTML = letters;
}
function myFunction() {
text = prompt("enter TODO");
list.push(text);
update();
}
Use the CSS
property list-style-position
to position the bullet:
list-style-position:inside /* or outside */;