javascript - I'm trying to add "grades" to an array and it isn't going as planned -



javascript - I'm trying to add "grades" to an array and it isn't going as planned -

the thought come in "grade" or general number, check create sure number, advertisement grade array, , each number added create list item containing input contains entered number.

i have 2 functions. first 1 called ,check(), assigns input value userinput variable, checks variable number. if calls adto() function supposed force userinput variable grade[] array update list. not happens. here's code:

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>ultimate grader</title> <style> </style> <script> function check(){ var userinput = document.getelementbyid('grade').value; if ( isnan(userinput) || userinput === " ") { alert("please come in number"); }else { adto(userinput); }; } function adto(input) { var grade = []; grade.push(input); (i=0; i<=grade.length; i++){ document.getelementbyid("list").innerhtml = "<li><input type='number' value='" + grade[i] + "'/></li>"; }; }; </script> </head> <body> <form> <input type="text" id="grade" name="grade" placeholder="grade"> <input type="button" id="add" name="add" value="add" onclick="check()"> </form> <ul id="list"> </ul> </body> </html>

any ideas?

every time phone call adto(), you're creating new, empty array. need define variable outside function, retain value between calls.

also, for() loop replacing innerhtml each time through loop, it's not appending it. utilize += append.

var grade = []; function adto(input) { grade.push(input); var list = document.getelementbyid('list'); list.innerhtml = ''; // empty (var = 0; < grade.length; i++) { list.innerhtml += "<li><input type='number' value='" + grade[i] + "'/></li>"; } }

demo

there isn't much need loop, though. since list should have items previous calls, need append new one:

function adto(input) { grade.push(input); document.getelementbyid('list').innerhtml += "<li><input type='number' value='" + input + "'/></li>"; }

demo

javascript arrays user-input

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -