I am having trouble using my spinIt method (my name for the method to play the audio clip). I cannot get it to target the audio element on a variable basis.
I would really like line 60 to be something like let clip = this.childElement
or something like that, but I can’t find anything like that out there. I have tried to use getElementById to get the keyName from my sounds array in my state object, but I’m not able to target a particular item within the array when I use the variable i, because the spinIt method is outside of the .map loop. I’ve been at this for hours but I can’t find anything that will let me put something in the getElementById paranthesis that responds to and changes with whichever Div I click on.
Here is a link to a fork of what I have so far on codepen: https://codepen.io/siege211/pen/QBqyEW?editors=0111
And here’s the javascript so far:
const catSounds = [{
keyName: 'Q',
keyCode: 81,
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-1.mp3'
}, {
keyName: 'W',
keyCode: 87,
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-2.mp3'
}, {
keyCode: 69,
keyName: 'E',
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-3.mp3'
}, {
keyCode: 65,
keyName: 'A',
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-4.mp3'
}, {
keyCode: 83,
keyName: 'S',
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-5.mp3'
}, {
keyCode: 68,
keyName: 'D',
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-6.mp3'
}, {
keyName: 'Z',
keyCode: 90,
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-7.mp3'
}, {
keyCode: 88,
keyName: 'X',
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-8.mp3'
}, {
keyName: 'C',
keyCode: 67,
url: 'http://audiosoundclips.com/wp-content/forum/uploads/2015/01/Cat-meow-9.mp3'
},
];
class App extends React.Component{
constructor(props) {
super(props);
this.state= {
sounds: catSounds,
padNumber: 0
}
//this bindings here
this.spinIt = this.spinIt.bind(this);
}
spinIt() {
let clip = document.getElementById('E');
clip.play();
console.log(this.state.sounds);
}
//event handlers here, including render method
render() {
const PADS = this.state.sounds.map(
(x,i) => {
return (
<div
className='drum-pad'
id={'clip'+i}
onClick={this.spinIt}>{x.keyName}
<audio
className='clip'
id={x.keyName}
src={x.url}
ref={
function(el) {
self._input = el;
}
}>
</audio>
</div>)});
return (
<div id='theMachine'>
<div id='display'>Display</div>
{/*<Pads />*/}
{PADS}
</div>
);}
}
ReactDOM.render(<App />, document.getElementById('drum-machine'));