I'm new to jQuery. I want to display content of a flash card when the page does the first loading, and users press Spacebar to show the answer. After users choose right or wrong, then the answer part will disappear. I got stuck at displaying and hiding the answer, also incrementing the index for loop.
Here is what I have done so far. I really appreciate any help!
$(document).ready(function() {
var list = new Array();
//get JSON from server
$.get( "classes/getJSON.php", function( data )
{
var parsedData = $.parseJSON(data);
list = parsedData;
return list;
});// end get
// loop through the array and display content
var index = 0;
window.on("keypress", function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
// press Spacebar to show answer
if(keycode == "32")
{
$(".cards_back").show();
}
//press arrow key to choose right or wrong
else if (keycode == "37")
{
index +=1 ;
var div_idword = "<span>" + list[index] + "</span>";
$(". audio").html(div_idword);
var $idword = list[index].idword;
var $box_num = parseInt(cards[index].box_num) + 1;
$(".cards_back").hide();
}
else if (keycode == "39")
{
index +=1 ;
var div_idword = "<span>" + cards[index].audio + "</span>";
$(". audio").html(div_idword);
var $idword = cards[index].idword;
var $box_num = parseInt(cards[index].box_num) - 1;
if($box_num < 0){
$box_num = 0;
}
$(".cards_back").hide();
}
$.ajax(
{
url : "classes/update_box_num.php",
type : "POST",
data : {'idword':$idword, 'box_num':$box_num},
success : function(data, textStatus, jqXHR){
alert("changed");
}
}
);// end ajax
});// end onclick
}); // end ready