totally beginner-question right here. Started studying front-end dev a month ago and now that i've reached Javascript, got working on my first practice projects. In this case, I want to create this really simple guitar tuner where you click a button and it executes its correspondent string.
This below is what i got so far, but can't express how to get the id of the button clicked. And also, can't figure out how to end one note's loop when clicking on another button.
const keyS = document.addEventListener('keydown', function(e){
(e.key === 'd') ? this.getElementById ("string6").play()
:(e.key === 'f') ? this.getElementById ("string5").play()
:(e.key === 'g') ? this.getElementById ("string4").play()
:(e.key === 'j') ? this.getElementById ("string3").play()
:(e.key === 'k') ? this.getElementById ("string2").play()
:(e.key === 'l') ? this.getElementById ("string1").play()
: alert("That's not a valid key!")
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles/main.css">
<title>Guitar Tuner</title>
<script src="scripts/main.js"></script>
</head>
<body>
<ul>
<li><audio id="string" src="sounds/string6.mp3" type="audio/mpeg" controls loop></audio></li>
<li><audio id="string5" src="sounds/string5.mp3" type="audio/mpeg" controls loop></audio></li>
<li><audio id="string4" src="sounds/string4.mp3" type="audio/mpeg" controls loop></audio></li>
<li><audio id="string3" src="sounds/string3.mp3" type="audio/mpeg" controls loop></audio></li>
<li><audio id="string2" src="sounds/string2.mp3" type="audio/mpeg" controls loop></audio></li>
<li><audio id="string1" src="sounds/string1.mp3" type="audio/mpeg" controls loop></audio></li>
<br>
<button onclick="clickNote()">6th String</button>
<button onclick="clickNote()">5th String</button>
<button onclick="clickNote()">4th String</button>
<button onclick="clickNote()">3rd String</button>
<button onclick="clickNote()">2nd String</button>
<button onclick="clickNote()">1st String</button>
</ul>
</body>
</html>
I know probably is a really dumb question, but I want to understand how this works properly to then start fiddling with it.
Thank you all in advance, and any JS learning or front-end career developing tips are very much welcome!
P.S: May you forgive any gramatical errors, for english is not my mother's tongue.