Hi all,
I have just started the drum machine project and i can have the sound played
Can someone tell me if the code is correct / why is not working?
I tried 2 methods and none of them works
1- create a function and associate it with an event attribute on the button element
2-in Jquery, just get the id element and play it
Play is a DOM native function, there’s no equivalent for JQuery (as far as I know, been a while since using it).
So either you get the dom element from the Jquery wrapper, or use standard JS:
document.getElementById("C").play()
The first line of the test suite states:
You can use any mix of HTML, JavaScript, CSS, Bootstrap, SASS, React, Redux, and jQuery to complete this project. You should use a frontend framework (like React for example) because this section is about learning frontend frameworks
Hi,
thanks for your reply
yes, I know that I am allowed to use Jquery but my question was if Jquery (alone) can fulfil the requirements to create the project
as regarding the play() method, I used a simple DOM snippet but i cannot hear any sound
x = document.getElementById("#C");
function playAudio() {
x.play();
}
hi,
yes, after some googleing I came up with the get(0) (or [0]) to have the audio played in jQuery
if you please give a look at this pen i made just for testinghttps://codepen.io/ranran212/pen/QWpbzrm
then i tried to create a function and use it in the onclick attribute of the button but when I click on the button i get this error
Uncaught ReferenceError: playSound is not defined
(I also tried this other syntax
const playSound = () => {
$(’.sound2’).get(0).play();
} but with same poor result;
$(document).ready(function(){
function playSound() {
$('.sound2').get(0).play();
};
})
by the way, what is the correct way to associate function with onclick attribute?
A- or
B-?
since I have several button, each associated with its own sound, which method is better: having the onclick attribute on the or the button.click(function{})?
$('button')
//this selects all tag with button into a list,
//a type of jQuery list object
When you add the onclick to this selector - then all button(s) will be clicked
A few suggestion:
Give each audio element a unique id and have a separate function to deal with each button click to point for respective autio file.
Or change what is happening whe a button is clicked
These two line of code are not being used together
let x=$(this).children("audio").attr(class);
//assign attribute value of current clicked element find child audio and get class name
$('.sound2').get(0).play();
//find class .sound2 and get first jquery list object convert to dom object and then play
Suggest find the dom element - like what you are doing in line 1
and then play it - like in line 2 (instead of playing .sound2)
Best practice syntax for onclick - I think is like following:
$(seclectorHere).on('click', function(){
//code here
});
//this syntax allows for many actions to be performed
//for example 'click mousedown', the events click and mousedown at the same time
thank you for your assistance
Actually I understood the selector references for both css and jQuery, I am just awkward about methods and some synthax error
for example I do not undestand why, if a define a function, I cannot use it on the onclick attribute for a button
Ah you can call a function via an onclick event.
It is probably due to scope that the function might not be found.
Basically if the function is defined in $(document).ready it is within the scope of the ready function.
Then when you try to call it outside of the $(document).ready scope it can not find it.