Below script is that when press the button, the browser speaks the variable word.
However, I use @foreach in laravel blade.
So the only first button works. But rest of buttons generated by foreach doesn't work.
index.blade.php
@foreach($data as $val)
<h2><a class="word" href="/?keyword={{$val->word}}">{{$val->word}}</a></h2>
<input id="text" type="hidden" value="{{$val->word}}">
<button id="speak-btn">play</button>
@endforeach
<script>
const text = document.querySelector('#text')
const speakBtn = document.querySelector('#speak-btn')
speakBtn.addEventListener('click', function() {
const uttr = new SpeechSynthesisUtterance(text.value)
speechSynthesis.speak(uttr)
})
</script>
So how can I make all the buttons work? I think I need to use this in javascript. However, I'm new to JS and I don't know how to properly use it. Thank you.