javascript - object HTMLInputElement error when trying to display random task -
so trying display , random string array created. counting portion of message works, second part should display , random task array shows error. did not use parseint , math.random function correctly? dont have round number since parseint converts integer right?
var tasks = []; // function called when form submitted. // function adds task global array. function addtask() { 'use strict'; // task: var task = document.getelementbyid('task'); // reference output goes: var output = document.getelementbyid('output'); // output: var message = ''; if (task.value) { // add item array: tasks[tasks.length] = task; var randomnum = tasks.length; var randomtask = parseint((math.random() * randomnum), 10); var randommsg = tasks[randomtask]; // update page: message = 'you have ' + tasks.length + ' task(s) in to-do list.\n'; message += 'random task: ' + randommsg; if (output.textcontent !== undefined) { output.textcontent = message; } else { output.innertext = message; } } // end of task.value if. // return false prevent submission: return false; } // end of addtask() function. // initial setup: function init() { 'use strict'; document.getelementbyid('theform').onsubmit = addtask; } // end of init() function. window.onload = init;
here:
tasks[tasks.length] = task;
you adding reference input array. each time runs, add reference same input. perhaps mean:
tasks[tasks.length] = task.value;
so tasks array of task names. rest should fine, though i'd use:
tasks.push(task.value);
and
if (typeof output.textcontent == 'string') { output.textcontent = message; } else { output.innertext = message; }
Comments
Post a Comment