sw45acp Posted November 2, 2009 Share Posted November 2, 2009 Hi, I am trying to create a very simple flash card script. Problem is, I can't find any examples on how to navigate the array containing the questions. When a person clicks next it will show the answer to the question, then next again to show the question in the sequence. I have this code so far. cards = new Array( "question 1", "answer", "question 2", "answer" ); function prevcard() { ? } function nextcard() { ? } any help you can give would greatly be appreciated! Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/ Share on other sites More sharing options...
sw45acp Posted November 2, 2009 Author Share Posted November 2, 2009 I kind of just figured out a simple one. It writes to a textbox: <html> <head> <script type="text/javascript"> var stuff = new Array() var which = 0; stuff[0] = "one"; stuff[1] = "two"; stuff[2] = "three"; //previous card function prevcard() { if (which>0) { which --; theform.key.value = stuff[which]; } } //next card function nextcard() { if (which<stuff.length-1) { which ++; theform.key.value = stuff[which]; } } </script> </head> <body> <form action="" name="theform"> <input type="text" name="key" /><br /> <input type="button" name="back" value="Previous card" onClick="prevcard()" /> <input type="button" name="next" value="Next card" onClick="nextcard()" /> </form> </body> </html> However, I can't get it write to div element instead of a textbox. I am accessing it via id and using innerHTML. Can't get it to work... <style type="text/css"> div { width: 200px; height: 150px; border: 1px solid #FF0000; } </style> <script type="text/javascript"> var x = document.getElementById("mainbox"); var stuff = new Array() var which = 0; stuff[0] = "one"; stuff[1] = "two"; stuff[2] = "three"; function prevcard() { if (which>0) { which --; x.innerHTML = stuff[which]; } } function nextcard() { if (which<stuff.length-1) { which ++; x.innerHTML = stuff[which]; } } </script> </head> <body> <div id="mainbox"></div><br /> <input type="button" name="back" value="Previous card" onClick="prevcard()" /> <input type="button" name="next" value="Next card" onClick="nextcard()" /> Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-949128 Share on other sites More sharing options...
Alex Posted November 2, 2009 Share Posted November 2, 2009 I wrote this while you were writing your solution... Maybe it'll help, if you need an explanation just ask. <script type="text/javascript"> var qnum = 0; var anum = 0; var questions = new Array('Question 1', 'Question 2', 'Question 3'); var answers = new Array('Answer 1', 'Answer 2', 'Answer 3'); function nextStep() { if(anum == answers.length) qnum = anum = 0; if(qnum == anum) { document.getElementById("question").innerHTML = questions[qnum++]; document.getElementById("answer").innerHTML = '???'; document.getElementById("next").value = 'Next Answer'; } else { document.getElementById("answer").innerHTML = answers[anum++]; document.getElementById("next").value = 'Next Question'; } } </script> <div> Question: <span id="question"></span><br /> Answer: <span id="answer"></span><br /> <input type="submit" value="First Question!" id="next" name="next" onclick="nextStep();" /> </div> Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-949131 Share on other sites More sharing options...
sw45acp Posted November 2, 2009 Author Share Posted November 2, 2009 This will work just fine! However, can we put in a back button to go back one card at a time also? Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-949134 Share on other sites More sharing options...
sw45acp Posted November 2, 2009 Author Share Posted November 2, 2009 I added this but it after going back 2 the next button has to be clicked twice which is odd function backStep() { if (qnum == anum) { qnum -- ; anum -- ; document.getElementById("question").innerHTML = questions[qnum]; document.getElementById("answer").innerHTML = '???'; } else { qnum --; document.getElementById("question").innerHTML = questions[qnum]; } } Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-949147 Share on other sites More sharing options...
Adam Posted November 2, 2009 Share Posted November 2, 2009 Perhaps try this.. var qna = [['question 1', 'answer 1'], ['question 2', 'answer 2'], ['question 3', 'answer 3']]; var i = 0; window.onload = function() { display(); } function backStep() { i = (i == 0) ? qna.length-1 : i-1; display(); } function nextStep() { i = (i == qna.length-1) ? 0 : i+1; display(); } function display() { document.getElementById('question').innerHTML = qna[i][0]; document.getElementById('answer').innerHTML = qna[i][1]; } Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-949364 Share on other sites More sharing options...
sw45acp Posted November 3, 2009 Author Share Posted November 3, 2009 This one works great. Thank you. I'm just a little confused on next and back functions on how exactly the code works there. I know with php that the ? and : means that: if x=2 ? (do this), if not : do that. Is that the same here? Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-950458 Share on other sites More sharing options...
Adam Posted November 3, 2009 Share Posted November 3, 2009 Yeah, exactly the same.. Link to comment https://forums.phpfreaks.com/topic/179898-solved-simple-flashcard-script/#findComment-950569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.