How can I store key presses into variables to refactor this code?

charliework

I'm trying to build a simple piano so a keypress plays the corresponding sound. I have data attributes set up in my html and on the audio elements like so:

<div class="piano-keys">
      <div data-key="65" class="wkey" id="key"></div>
        <div data-key="81" class="bkey" id="key></div>
</div>

<audio id="65" data-key="65" src="notes/261-C.mp3"></audio>
<audio id="83" data-key="83" src="notes/293-D.mp3"></audio>

and my jquery is as follows:

 $(document).keyup(function(e){
         if(e.keyCode == 65){
             var c = document.getElementById('65');
             c.play();
             c.currentTime = 0;
      } else if(e.keyCode == 83){
            var c = document.getElementById('83');
             cSharp.play();
             cSharp.currentTime = 0;
       }
    });

This obviously works but I will need 25 if/else and I know it can be done a lot cleaner. Instead of having to target the individual ID's. I tried to create variables for they key presses like this:

var key = $('.key[data-key="${e.keyCode}"]');

but that obviously never worked. I'm new to jquery if that hasn't become painfully obvious by the stage.

CumminUp07

First of all, you can't have the same ID multiple times on one page, other than that you can just do something like this

$(document).keyup(function(e){
    var key = e.which;
    var note =document.getElementById(key);
    note.play();
    note.currentTime = 0;


})

it will play the corresponding audio to which ever key is pressed. I'm not sure what you are doing with currentTime but you didn't mention it in your question, so this need adapting to do whatever it is that you are doing with that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I catch 2+ key presses at once?

From Dev

How can I play a sound when a user presses a button in a MessageBox?

From Dev

How can I refactor this code that finds the end of Unix time?

From Dev

How can I write a function that will create and store variables?

From Dev

How can I use ESLint to refactor/restyle code in webstorm

From Dev

How can I refactor a set of ugly if statements?

From Dev

How can I refactor this code to be more concise?

From Dev

How can I store a Bouncy Castle PGP key in a Java Keystore?

From Dev

How can I refactor Model to resolve code warnings: "CA2214" and "CA2227"?

From Dev

PHP How can I store variables in an array and call them

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How can I emulate key presses on Vim startup?

From Dev

How to detect global key presses

From Dev

How can I refactor / make this code more pythonic?

From Dev

How can I prevent key presses from changing the value of a scrollbar control?

From Dev

How can I use the Factory pattern to refactor my Java code?

From Dev

Can I refactor this code in any way in javascript?

From Dev

how to proxy key presses

From Dev

How can i refactor these lines of code?

From Dev

How can I refactor Model to resolve code warnings: "CA2214" and "CA2227"?

From Dev

PHP How can I store variables in an array and call them

From Dev

How can I refactor these variables in React.js

From Dev

How can I store values of buttons in variables? (Python Gtk)

From Dev

How can I iterate each time the user presses a button?

From Dev

How can I store strings with variables in them in another Java class?

From Dev

How can I print a string when the user presses the enter key whilst playing my guessing game?

From Dev

How can I map repeated key presses to specific?

From Dev

How Can I Refactor To Avoid Duplication?

From Dev

PaperJS variables, how can I store these in MongoDB?

Related Related

  1. 1

    How can I catch 2+ key presses at once?

  2. 2

    How can I play a sound when a user presses a button in a MessageBox?

  3. 3

    How can I refactor this code that finds the end of Unix time?

  4. 4

    How can I write a function that will create and store variables?

  5. 5

    How can I use ESLint to refactor/restyle code in webstorm

  6. 6

    How can I refactor a set of ugly if statements?

  7. 7

    How can I refactor this code to be more concise?

  8. 8

    How can I store a Bouncy Castle PGP key in a Java Keystore?

  9. 9

    How can I refactor Model to resolve code warnings: "CA2214" and "CA2227"?

  10. 10

    PHP How can I store variables in an array and call them

  11. 11

    How can I refactor my kotlin code and make it more clear

  12. 12

    How can I emulate key presses on Vim startup?

  13. 13

    How to detect global key presses

  14. 14

    How can I refactor / make this code more pythonic?

  15. 15

    How can I prevent key presses from changing the value of a scrollbar control?

  16. 16

    How can I use the Factory pattern to refactor my Java code?

  17. 17

    Can I refactor this code in any way in javascript?

  18. 18

    how to proxy key presses

  19. 19

    How can i refactor these lines of code?

  20. 20

    How can I refactor Model to resolve code warnings: "CA2214" and "CA2227"?

  21. 21

    PHP How can I store variables in an array and call them

  22. 22

    How can I refactor these variables in React.js

  23. 23

    How can I store values of buttons in variables? (Python Gtk)

  24. 24

    How can I iterate each time the user presses a button?

  25. 25

    How can I store strings with variables in them in another Java class?

  26. 26

    How can I print a string when the user presses the enter key whilst playing my guessing game?

  27. 27

    How can I map repeated key presses to specific?

  28. 28

    How Can I Refactor To Avoid Duplication?

  29. 29

    PaperJS variables, how can I store these in MongoDB?

HotTag

Archive