arrays - JavaScript Pop/Shift issue -
basically when user enters in "add task" field , presses add task button, things entered displayed in task list, , when user clicks "show next task", should take last thing entered in task list array, , display in "next task" text box.
my code:
var tasklist = []; var $ = function (id) { return document.getelementbyid(id); } var addtask = function() { var newtask = $("new_task").value; tasklist.push(newtask); display(); } var display = function() { var outstring = ""; document.getelementbyid("new_task").value = ""; (var in tasklist) { outstring += tasklist[i] + "\n"; } $("task_list").value = outstring; } var finaltask = function() { document.getelementbyid("new_task").value = ""; document.getelementbyid("next_task").value = tasklist; var nexttask = $("task_list").value; tasklist.shift(nexttask); display(); if (nexttask == "") { alert("no tasks remaining"); } } window.onload = function() { $("add_task").onclick = addtask; $("show_next_task").onclick = finaltask; }
it looks calling method wrong name. have:
window.onload = function () { $("add_task").onclick = addtask; $("show_next_task").onclick = nexttask; // <------------- }
but named function nexttasks
.
edit:
you have error in nexttasks
method:
var nexttasks = function() { var nexttask = $("next_task").value; tasklist.shift(nexttasks); // <---------------- display(); }
at arrow seems passing function argument, , meant pass var nexttask
.
you save lot of headaches giving variables , functions names easier differentiate between. might seem tedious, kind of error pretty common.
Comments
Post a Comment