I want to make a page when for each letter key i pressed to display something (an image and a paragraph for now). I can do it by repeating my code for each key, and this isn't how i want it, so i think i need a loop but i can not figure it out how to do it.
I can do it like this:
$(document).keydown(function(event) {
if (event.keyCode == 65) {
$("#paragraph").text("text1");
$('#divImg').html('img1');
}
if (event.keyCode == 83) {
$("#paragraph").text("text2");
$('#divImg').html('img2');
}
if (event.keyCode == 68) {
$("#paragraph").text("text3");
$('#divImg').html('img3');
}
}
);
I want to do it by using a loop and accesing an object array like:
var keys = {
a: {
par: "text1",
img: "img1"},
s: {
par: "text2",
img: "img2"
},
d: {
par: "text3",
img: "img3"
}
}
and instead of repeating my code 25 times for each key(i dont want 25 if statements), i want my code to figure it out what key i pressed and to get his image and his paragraph from the object. I can't figure it out a way to make it. I tried to convert the keycode into a character but after a while i get stuck into the code.
I hope I've been more explicit this time, and I apologize if I made any confusion.