html - Not able to access next element of an array javascript -
i new js, trying access next element of array using onclick function no luck.
var i, len; function quiz() { var quiz_questions = [ "who founder of facebook?", "who founder of google?" ]; len = quiz_questions.length; for(i = 0; < len; i++) { document.getelementbyid("demo").innerhtml = quiz_questions[i]; } } <button onclick="quiz();">click</button> <p id="demo"></p> can explain me did wrong here.
your problem is, iterate through every question, every time click. remove loop , put i++ after setting question. maybe checking, if less array length before it, wouldnt bad idea.
var i, len; = 0; function quiz() { var quiz_questions = [ "who founder of facebook?", "who founder of google?" ]; len = quiz_questions.length; document.getelementbyid("demo").innerhtml = quiz_questions[i]; i++; if(i>=len){ i=0; } } <button onclick="quiz();">click</button> <p id="demo"></p>
Comments
Post a Comment