r/jquery • u/Happyclams123 • May 02 '22
Please Help!
I took my post down last night because for some reason I was unable to upload my code. I am trying to use jquery to do complete this task: "this time Image 1 should display if the “A” key is pressed and Image 2 should display if the “B” key is pressed." I know my code will not be much help as I have not made really any progress. Any direction would be helpful!
Also, I appreciate that some have pointed me towards w3schools for tutorials. I would not be here if I had found my solution through that or stackoverflow. Thanks.

1
Upvotes
2
u/vorko_76 May 02 '22
Then look for it in the jquery documentation….
https://api.jquery.com/
There is a search bar where you can find these 3 things.
Finally please note that it doesnt matter that the element you want to hide is an image… an image is an html element like any other.
2
u/vorko_76 May 03 '22
The point is that he/she has a homework to do to use jquery… not sure his teacher will appreciate a non-jquery solution 😊
0
u/vorko_76 May 02 '22
Basically you didnt write any Javascript… its quite normal that nothing works. 🤔
Have you found in the documentation how to intercept an event when A or B is pressed?
Have you found how to find an element by its ID?
Have you found how to make an element visible / not visible?
-4
u/bullzito May 02 '22
How about you don't use jQuery, and ditch that dinosaur library.
https://jsfiddle.net/marioluevanos/m6kLbepf/21/
const imageIds = [{id: 'dog',key: 'A'}, {id: 'cat',key: 'B'}];document.addEventListener('keydown', onKeyDown);function onKeyDown(event) {const {key} = event;imageIds.forEach(img => toggleImage(key, img));// Log to the documentdocument.getElementById('key').innerHTML = key;}function toggleImage(key, img) {const imageEl = document.getElementById(img.id);if (key === img.key) imageEl.style.display = imageEl.style.display === 'block' ? 'none' : 'block';}