javascript - Making a list in HTML from value of textarea -
i need make list using javascript.
what i’m trying if user clicked submit button elements in textarea making list in html.
html:
<body> <div class="container"> <div id="main"> <img src=""> </div> </br> <div class="box"> <h3>guest list:</h3> <textarea id="text"></textarea> </br> <button id="submit" onclick="submit()">submit</button> </div> <div class = "guestlist"> <div id="list"></div> </div> <script src="main.js"></script> </body>
javascript:
function submit(){ var guestname = document.getelementbyid('text'); var listdata = [guestname]; var listcontainer = document.getelementbyid('list'); document.getelementsbytagname('body')[0].appendchild(listcontainer); var listelement = document.createelement("ul"); guestname.appendchild(listelement); var numberoflistitems = listdata.length; for(var = 0; < numberoflistitems; ++i){ var listitem = document.createelement("li"); listitem.innerhtml = listdata[i]; listelement.appendchild(listitem); } }
alright, here’s fixed code , below explanations, etc.
full fixed code along other improvements
function submit(){ var guestname=document.getelementbyid('text'); var listdata=guestname.value.split('\n'); var listcontainer=document.getelementbyid('list'), listelement=document.createelement("ul"); listcontainer.appendchild(listelement); var numberoflistitems=listdata.length; var listitem; for(var i=0; i<numberoflistitems; ++i){ listitem=document.createelement("li"); listitem.innerhtml=listdata[i]; listelement.appendchild(listitem); } }
<body> <div class="container"> <div id="main"> <img src="" alt=""/> </div> <br/> <div class="box"> <h3>guest list:</h3> <textarea id="text"></textarea> <br/> <button id="submit" onclick="submit();">submit</button> </div> <div class="guestlist"> <div id="list"></div> </div> </div> <script src="main.js"></script> </body>
explanations
numberoflistitems = listdata.length;
this 1. line defining listdata
problematic:
var listdata = [guestname];
listdata
array containing textarea element.
the proper way of getting individual names out of textarea split()
value of textarea. i’m going assume guest list in textarea separated line breaks write proper code:
var guestname = document.getelementbyid('text'); // assigns element guestname var listdata = guestname.value.split('\n'); // listdata array contains every line in textarea
this makes use of string.prototype.split
takes delimiter argument , turns string array based on separators.
this following line re-appends element present in dom… dom… kind of unnecessary:
document.getelementsbytagname('body')[0].appendchild(listcontainer);
this line problematic well:
guestname.appendchild(listelement);
<textarea>
s can’t have child elements. wanted append child listcontainer
.
the loop adds <li>
element <ul>
per “guest”. should work fine now other flaws fixed.
a few remakrs html:
<img src="">
should @ least<img src="" alt=""/>
— a missingalt
attribute invalid- each
</br>
should<br/>
- the
</div>
<div class="container">
missing
Comments
Post a Comment