2
Test scenario
I have a page with the following elements:
<audio id="aud1" src="audio/1.ogg" type="audio/ogg"></audio>
<audio id="aud2" src="audio/2.ogg" type="audio/ogg"></audio>
<audio id="aud3" src="audio/3.ogg" type="audio/ogg"></audio>
And a function:
$.ajax({
url : "x.php",
type : "post",
dataType: "text",
data : data,
cache: false,
success : function(response) {
if (response == '1') {
document.getElementById('aud1').play();
} else if (response == '2') {
document.getElementById('aud2').play();
} else if (response == '3') {
document.getElementById('aud3').play();
} else if (response == 'E'){
alert('Erro.')
}
},
error : function() {
alert('error');
}
})
According to the return of the request by ajax, the function will give play on audio. Working perfectly.
Problem
When I use $('#aud1').play()
, returns me the following error:
Uncaught Typeerror: $(...). play is not a Function
Doubt
- There is a way to "play" audio via jQuery?
About the selector, it was failure in typing the same question rs... About your solution, it really worked +1... But I would like to know why to use the
[0]
? Would be the reason when I execute$('#aud1')
, he creates aarray
(or obj? ) element data? Or something like!?– rbz
A jQuery element can load multiple DOM elements, for example, when I call
$('div')
, I will have in hand all the Ivs of the page, and in case I do$('div').addClass('teste')
, I’ll be adding the test class to all of them.– Francisco
So, but if I call
$('#aud1')
because then I need the[0]
? What would you have in case[1]
then!? I don’t understand if it "undoes" the element data, or what it does...– rbz
As I said, the jQuery element loads multiple DOM elements. If you access index 0 with
[0]
you are simply accessing the first element of this list, with[1]
, the second, and so it goes. If you want to modify in all, you will have to iterate on them.– Francisco