why doesn’t my audio work? the Q button should be setup to make cow noises
So a few issues here:
- You need to bind functions to a stateful class component, or else
this.playSound
does not work. Use:
this.methodName=this.methodName.bind(this);
-
.getElementsById('audioQ')[0]
does not work because it isgetElementById
since id is unique, and consequently the index[0]
should also be removed -
in the
<button>
, it should be `id=‘audioQ’ with lowercase i. -
in the
<button>
, it should beonClick={this.playSound}
with uppercase C. -
the src while points to a valid mp3, does not play. When I used one of the sounds used in the sample drum machine it does:
https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3
.
Hi there,
There are a few problems with your code:
The used event in the button must be onClick
and not onclick
.
In addition, document.getElementsById
is not a function. You probably meant document.getElementById
which returns only one element by unique id, so you don’t need to index it anymore.
Solving these two will set you on track and your sound will play.
Hope that helps.
all good now! thanks so much guys! more to come, no doubt
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.