Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

myaudio is not a function

I got two divs with audio files:

        <div class="ru">
        <audio class ="audio-play" src = "hadeWomA.mp3" type="audio/mpeg"></audio>
      </div>
    <div class="ru">
      <audio id ="audio-play" src = "hadeWomB.mp3" type="audio/mpeg"></audio>
    </div>

Im trying to get it to play the one I press on, so i made this:

$(".ru").click(function()
{   
    var myAudio = $(this).children("audio");
    myAudio.play();
});

What am I missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I directily target the audio like this it works

    var myAudio = this.document.getElementById("audio-play");

>Solution :

play() is not a jQuery method, it’s a vanilla JS method and it works with a single element passed, however jQuery selector returns you a set of a elements. You should pick the first one:

myAudio[0].play();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading