JavaScript - Turning an array into a list -
i want able turn array, consists of names entered user, unordered list list presents names on first list item. how separate names onto different list items?
this have:
function createlist(){ var list = document.getelementbyid("list"); var li = document.createelement("li"); var input = document.getelementbyid("q8").value; var strcomma = input.split(","); console.log(strcomma); var = 0; (i; < strcomma.length; i++){ var el = document.createtextnode(strcomma[i]); li.appendchild(el); list.appendchild(li); } }
that should trick
function createlist(){ var list = document.getelementbyid("list"); //var li = document.createelement("li"); var input = document.getelementbyid("q8").value; var strcomma = input.split(","); console.log(strcomma); var = 0; (i; < strcomma.length; i++){ var el = document.createtextnode(strcomma[i]); var li = document.createelement("li"); li.appendchild(el); list.appendchild(li); } }
that way creating new li
element , appending same existing ul
element. in way, declared li
outside loop, referenced same li
element every time.
Comments
Post a Comment