0

I would like to click in the words that is in the array with texts and sounds.

I took the code from this link: Detect which word has been clicked on within a text, but I don't know how to figure out a way to click in the word and listen its sound using jquery

<script>
var wordaudio = [];
wordaudio["Lorem"] = "lorem.mp3";
wordaudio["ipsum"] = "ipsum.mp3";
wordaudio["dolor"] = "dolor.mp3";

$("body").click(function(){
     var s = window.getSelection();
    s.modify('extend','backward','word');        
    var b = s.toString();

    s.modify('extend','forward','word');
    var a = s.toString();
    s.modify('move','forward','character');
  //   alert(b+a);
  document.getElementById('myAudio').src='0001.mp3';
  document.getElementById('myAudio').play();


});
</script>

<p>Lorem ipsum dolor sit amet.</p>
<audio id="myAudio" src=""></audio>
Community
  • 1
  • 1
  • Would adding spans around the words be an option? – Teemoh May 14 '17 at 17:26
  • I know javascript, but I never used jQuery, this is so weird to me, I don't know what to do. I have a lot of audio files to learn a new language, I think I will try to do it in pure javascript. The script works well without use spans, you need to enable the alert function to see. I have signed today only to ask for it. I am a new member! – Edmilson Santos Da Silva May 14 '17 at 18:32

1 Answers1

0

I got to modify the script to pure javascript, unfortunately I don't know to use array in this example, I think I need to rename my all audio files... Only word horse works in this example.

function hearWords() {
var s = window.getSelection(); 
    s.modify('extend','backward','word');        
 var b = s.toString();
    s.modify('extend','forward','word');
 var a = s.toString();
    s.modify('move','forward','character');
var audio = b+a.trim()+'.mp3'; 
  document.getElementById('myAudio').src='https://www.w3schools.com/jsref/'+audio;
  document.getElementById('myAudio').play();
     alert(audio);
 }
<p id="words" onclick="hearWords();">no sound horse </p>
<audio id="myAudio" src=""></audio>