R Gao
R Gao

Reputation: 19

Javascript 30 "Drum kit" console.log Issue

I'm doing the JavaScript 30 and I have an issue with the beginning of the JavaScript Drum Kit. I have followed the code exactly, but I seem to have an issue with the the first fews lines of JavaScript in the program. I need it to console.log <audio data-key="(number)" src="/sounds/(name-of-sound.wav)</audio>, but I keep on getting "null" when I open console and press a key instead of <audio data-key="(number)" src="/sounds/(name-of-sound.wav)</audio>, which is my issue.

Here is my code:

html (omitted irrelevant parts):

              <div class="keys">
              <div data-key="65" class="key">
                  <kbd>A</kbd>
                  <span class="sound">Fired</span>
              </div>
              <div data-key="83" class="key">
                  <kbd>S</kbd>
                  <span class="sound">Long</span>
              </div>
              <div data-key="68" class="key">
                  <kbd>D</kbd>
                  <span class="sound">Quick</span>
              </div>
              <div data-key="70" class="key">
                  <kbd>F</kbd>
                  <span class="sound">Wet</span>
              </div>
              <div data-key="71" class="key">
                  <kbd>G</kbd>
                  <span class="sound">Messy</span>
              </div>
              <div data-key="72" class="key">
                  <kbd>H</kbd>
                  <span class="sound">Screech</span>
              </div>
              <div data-key="74" class="key">
                  <kbd>J</kbd>
                  <span class="sound">b0ss</span>
              </div>
              <div data-key="75" class="key">
                  <kbd>K</kbd>
                  <span class="sound">Splat</span>
              </div>
              <div data-key="76" class="key">
                  <kbd>L</kbd>
                  <span class="sound">Quack</span>
              </div>
       </div>

       <audio data-key="65" src="/sounds/fired.wav"></audio> 
       <audio data-key="83" src="/sounds/long.wav"></audio> 
       <audio data-key="68" src="/sounds/quick_one.wav"></audio> 
       <audio data-key="70" src="/sounds/wet_burst.wav"></audio> 
       <audio data-key="71" src="/sounds/messy.wav"></audio>
       <audio data-key="72" src="/sounds/screecher.wav"></audio> 
       <audio data-key="74" src="/sounds/ey_b0ss.wav"></audio> 
       <audio data-key="75" src="/sounds/splat.wav"></audio> 
       <audio data-key="76" src="/sounds/quack.wav"></audio> 

JavaScript:

   <script>
          window.addEventListener('keydown', function(e) {
          const audio = document.querySelector('audio[data- 
          key="${e.keyCode}"]');
          console.log(audio);
         });
   </script>

If anyone could help me make it log <audio data-key="(number)" src="/sounds/(name-of-sound.wav)</audio> instead of "null," I would greatly appreciate it.

PS: I use Google Chrome and Brackets text editor if that helps.

Upvotes: 0

Views: 471

Answers (1)

Fool
Fool

Reputation: 119

You have your keys on a div element, not an audio element.

So it should be something like:

     window.addEventListener('keydown', function(e) {
       let selector = `div[data-key='${e.keyCode}']`;
       let audio = document.querySelector(selector);
       console.log(audio);
     });

Also be careful with your quotation marks, if you want to interpolate variables into it then you need to use those slanted single quotes (ES6).

Hope that helps!

Upvotes: 0

Related Questions