Hey guys i have some code here that dynamically creates text boxes when a button is clicked however, i would like to take in the text box values by using getElementByClassName, Retrieve all of the "listitem" text fields into an array.Then I code a loop to find the values of each of those elements (ex. what the user entered) and put those values in an array. Then sort that array. I will then display them later on... Any help is appericated!
Javascript:
var $ = function (id)
{
return document.getElementById(id);
}
var sortItem = function ()
{
var myDisplayItems = "";
myDisplayItems.innerHTML = "";
var myClassTag = document.getElementsByClassName("listitem");
for (index in myClassTag)
{
myDisplayItems += "<br>" + myClassTag[index];
}
//sort Array
}
var addItem = function()
{
var myToDoList = $("todolist");
var myInput = document.createElement("input");
myInput.setAttribute("type", "text");
myInput.setAttribute("class", "listitem");
myToDoList.appendChild(myInput);
var myBr = document.createElement("br");
myDoToList.appendChild(myBr);
}
window.onload = function ()
{
$("additem").onclick = addItem;
$("sortitems").onclick = sortItem;
}
HTML:
<body>
<h1>ToDo List - Date: <span id='today'> </span></h1>
<div id="todolist">
<p>
<input type="button" id="additem" value="Add Item">
</p>
</div>
<hr>
<div>
<p>
<input type="button" id="sortitems" value="Sort and Display Items">
</p>
<p id="displayitems">
</p>
</div>
</body>
getElementsByClassName
, then loop through the values. If you have an implementation that doesn't work, you should post that code, and we could help. – Evan Oct 29 at 3:47