WHAT I WANT TO DO
I want to shorten my code. This Drum Play App plays sound by pressing certain keys or clicking with your mouse.
It works, but the code for click events is too long because I repeated the same code many times.
Could anyone make it cleaner/shorter?
WHAT I TRIED
I tried for loop, like below:
document.querySelector(`div[data-key="${dataKeys[i]}"]`).addEventListener...
But it didn't work.
MY CURRENT CODE
Here is my code.
<body>
<div class="keys">
<div data-key="65" class="key">
<kbd>A</kbd>
<span class="sound">clap</span>
</div>
<div data-key="83" class="key">
<kbd>S</kbd>
<span class="sound">hihat</span>
</div>
<div data-key="68" class="key">
<kbd>D</kbd>
<span class="sound">kick</span>
</div>
...
</div>
<audio data-key="65" src="sounds/clap.wav"></audio>
<audio data-key="83" src="sounds/hihat.wav"></audio>
<audio data-key="68" src="sounds/kick.wav"></audio>
...
<script>
window.addEventListener('keydown', function(e) {
const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
if (!audio) return; // Stop the function from running all together
audio.currentTime = 0; // Rewind to the start
audio.play();
});
document.querySelector('div[data-key="65"]').addEventListener('click', function(e) {
console.log('I clicked.')
const clickedAudio = document.querySelector(`audio[data-key="65"]`);
clickedAudio.currentTime = 0; // Rewind to the start
clickedAudio.play();
});
document.querySelector('div[data-key="83"]').addEventListener('click', function(e) {
console.log('I clicked.')
const clickedAudio = document.querySelector(`audio[data-key="83"]`);
clickedAudio.currentTime = 0; // Rewind to the start
clickedAudio.play();
});
document.querySelector('div[data-key="68"]').addEventListener('click', function(e) {
console.log('I clicked.')
const clickedAudio = document.querySelector(`audio[data-key="68"]`);
clickedAudio.currentTime = 0; // Rewind to the start
clickedAudio.play();
});
...
</script>
</body>
I'd appreciate it if anyone could make my code cleaner or shorter.
I want to make sure that I can't push a duplicated value into an array in a React state. The duplicated value is still going in the array though. I have tried using .includes but it is not working. ...
I want to make sure that I can't push a duplicated value into an array in a React state. The duplicated value is still going in the array though. I have tried using .includes but it is not working. ...
Problem Let's make a basic list and sort it to make sure that 2 is ALWAYS first in the list. Simple enough, right? [1, 2, 3].sort((a, b) => { if (a === 2) return -1; return 0; }); Chrome ...
Problem Let's make a basic list and sort it to make sure that 2 is ALWAYS first in the list. Simple enough, right? [1, 2, 3].sort((a, b) => { if (a === 2) return -1; return 0; }); Chrome ...
I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to ...
I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to ...
Following scenario/my solution consists of the following: Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...
Following scenario/my solution consists of the following: Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...