0

I'm trying to have each key, correspond to different images when pressed. (Ex: When "A" is pressed = image1 appears, When "S" is pressed = image2 appears, and so on.)

I was able to get the "S" Key to work, but not the A. I'm trying to do this for the whole row on the keyboard ("A","S","D","F","G","H","J","K","L")

*PS. Im still a beginner :)

This is my code:

<img src="images/giphy.gif" width="800" height="400" alt=""/>

<script>
  document.addEventListener("keydown", keyDownBody, false);

  function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==65) {
      myFunction();
    }
  }

function myFunction() {
var x = document.createElement("IMG");
x.setAttribute("src", "images/giphy1.gif")
x.setAttribute("width", "800");
x.setAttribute("height", "400");
x.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(x);
}

function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==83) {
      myFunction();
    }
  }

function myFunction() {
var x = document.createElement("IMG");
x.setAttribute("src", "images/sun.gif")
x.setAttribute("width", "800");
x.setAttribute("height", "400");
x.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(x);
}
</script>
3
  • better to pass the value of keyCode to myFunction and stored image into object where properties would be keyCode and value would be image src Commented Dec 5, 2016 at 19:37
  • 2
    @Mahi Why are you downvoting and criticizing all answers, instead of just writing your own? Commented Dec 5, 2016 at 19:48
  • @Duncan sorry . Commented Dec 5, 2016 at 19:51

4 Answers 4

2

You have declared the same function multiple times. You could merge them to make it work, and also make your code more readable. An example:

document.addEventListener("keydown", keyDownBody, false);

function keyDownBody(e) {
  var keyCode = e.keyCode;
  switch (keyCode) {
    case 65:
        loadImage("images/giphy1.gif", "The Pulpit Rock");
        break;
    case 83:
        loadImage("images/sun.gif", "The Pulpit Rock");
        break;

    // Etc.
  }
}

function loadImage(uri, alt) {
    var x = document.createElement("IMG");
    x.setAttribute("src", uri)
    x.setAttribute("width", "800");
    x.setAttribute("height", "400");
    x.setAttribute("alt", alt);
    document.body.appendChild(x);
}

This way you can add as many keys as you wish.

Alternatively, you could load the images dynamically if you put them in an array:

var images = [{
      uri: 'images/giphy1.gif',
      alt: 'A',
    }, {
      uri: 'images/sun.gif',
      alt: 'Some text',
    }, 
    // Etc.
];

function keyDownBody(e) {
  var keyCode = e.keyCode;

  var index = keyCode - 65;
  if (typeof images[index] !== 'undefined')
  {
    var image = images[index];
    loadImage(image.uri, image.alt);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2

var keys_to_div = [];
keys_to_div[65] = "1"; // A key
keys_to_div[66] = "2"; // B key

window.onkeydown = function(e) {
    if (keys_to_div[e.keyCode])
	document.getElementById(keys_to_div[e.keyCode]).style.display = "block";
}

window.onkeyup = function(e) {
    if (keys_to_div[e.keyCode])
	document.getElementById(keys_to_div[e.keyCode]).style.display = "none";
}
img {
  width: 50px;
  height: 50px;
  display: none;
}
<img src="http://images4.fanpop.com/image/photos/22100000/The-letter-A-the-alphabet-22186936-2560-2560.jpg" id="1">
<img src="http://www.drodd.com/images15/letter-b23.gif" id="2">

4 Comments

your fiddle is broken + like this array length would be 66 and you are using only 2 variable. so use object instead
How is it broken?
it's not displaying anything
@Mahi, You have to click on the area of the fiddle to focus it then press the keys that were set (A and B).
0

Use same keydown EventListener to all the keys like

document.addEventListener("keydown", keyDownBody, false);

  function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==65) {
      myFunction("images/giphy1.gif");
    }
    else if(keyCode==83) {
      myFunction("images/sun.gif");
    }
  }

function myFunction(t) {
var x = document.createElement("IMG");
x.setAttribute("src", t)
x.setAttribute("width", "800");
x.setAttribute("height", "400");
x.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(x);
}

Comments

0

You just need to add more conditions

function keyDownBody(e) {
    var keyCode = e.keyCode;
    if(keyCode==83) {
      myFunction("images/giphy1.gif");
    }

    if(keyCode==85) {
      myFunction("images/giphy2.gif");
    }

    if(keyCode==87) {
      myFunction("images/giphy3.gif");
    }
  }

function myFunction(imageName) {
  var x = document.createElement("IMG");
  x.setAttribute("src", imageName)
  x.setAttribute("width", "800");
  x.setAttribute("height", "400");
  x.setAttribute("alt", "The Pulpit Rock");
  document.body.appendChild(x);
}

2 Comments

same image would be appeared every time .
Sorry for the incomplete answer. But you can pass any parameter to change the myFunction behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.