Play audio in JQuery

0

In my HTML I have the following (in addition to the "display: none" in the css):

<audio class="audio">
    <source src="~/Audios/NUMEROS/ONE.ogg" type="audio/ogg">
</audio>

And to execute said Audio, I put the following code in my javascript file:

$(document).ready(function () {
    $(".audio").play();
});

But when I open the page, in console I get the following error:

Uncaught TypeError: audio.play is not a function

    
asked by Victor Matilla Sanchez 27.05.2018 в 17:48
source

1 answer

3

To execute the audio you have to specify its position in the array of elements that you have selected with jQuery. In this case it would be in such a way:

$(".audio")[0].play();

It is necessary to do it this way because the jQuery team decided not to add the play() function to all the elements, since it only affects those of audio .

Here is the official reasoning (in English): link

    
answered by 28.05.2018 / 03:33
source