I am trying to add audio to text to be played on click (in the background) and pause on click again (To read the text on the click)
The website is made with WordPress.
I have entered this script using wp code snippet plugin, and sat the functionality to be played over site header and in posts, I am trying to enter something like this
var yourAudio = document.getElementById('yourAudio'),
ctrl = document.getElementById('audioControl');
ctrl.onclick = function () {
// Update the Button
var pause = ctrl.innerHTML === 'pause!';
ctrl.innerHTML = pause ? 'play!' : 'pause!';
// Update the Audio
var method = pause ? 'pause' : 'play';
yourAudio[method]();
// Prevent Default Action
return false;
};
<audio id="yourAudio" preload="none">
<source src="https://cgejordan.com/wp-content/uploads/2023/01/0007.mp3" type="audio/wav" />
</audio>
<a id="audioControl" href="##">Arabic Word 1</a>
<audio id="yourAudio" preload="none">
<source src="https://cgejordan.com/wp-content/uploads/2023/01/0008.mp3" type="audio/wav" />
</audio>
<a id="audioControl" href="##">Arabic Word 2</a>
<audio id="yourAudio" preload="none">
<source src="https://cgejordan.com/wp-content/uploads/2023/01/0009.mp3" type="audio/wav" />
</audio>
<a id="audioControl" href="##">Arabic Word 3</a>
code reference: https://stackoverflow.com/a/7639343
But texts are not playing the audios when clicked
Could you please assist on how to make it work? and to keep the same text without changing it to Play! Pause! when clicked? just play and pause audio on word click
And do you suggest a better way to add audios into texts without making the page too large \slow?
I have tried many plugins but nothing worked as needed