Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 228

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T09:03:10+00:00 2024-11-25T09:03:10+00:00

How to Play Sound on Button Click in JavaScript

  • 62k

If you want to create JavaScript Play Sound On Click then this tutorial is for you. I have created a button here. When you click on that button, the sound will play.

This type of play sound on click you can use it everywhere in the webpage or in a certain element. In most cases we hear a beep sound in the button. So Beep Sound has been used here too. But you can use any other sound if you want. Watch its live demo to learn how it works.

✅ 100+ Best JavaScript Projects

Very little JavaScript has been used to create this Play Sound Button. If you know a little JavaScript you can easily create it.

HTML Code:
The button was created by the following html code. Here the button function of html is used.

<button>Click me!</button> 
Enter fullscreen mode Exit fullscreen mode

CSS Code:
Buttons are designed with some simple css. First I designed the webpage with some code. Then I designed the button and added the hover effect.

* { margin: 0; padding: 0; border: none; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }  html, body { height: 100%; }  body { display: flex; align-items: center; justify-content: center; background: #dff0f6; }  button { font-family: 'Open Sans', sans-serif; font-size: 1.5rem; color: #fff; background: #034d85; padding: 1.1rem 3.4rem; border-radius: .2rem; transition: opacity .2s; }  button:hover { cursor: pointer; background: #053056; } 
Enter fullscreen mode Exit fullscreen mode

JavaScript Code:
Now play sound on click html has been activated by JavaScript.

  • First line I added audio using JavaScript. Here I have used beep sound. You can use any audio.
  • Then a global constant of buttons is set.
  • Then the sound in the 'audio' is linked to the button by the onclick event. Then I have used the play () method here to play the audio. play () method starts playing the current audio.
const audio = new Audio("https://www.fesliyanstudios.com/play-mp3/387"); const buttons = document.querySelectorAll("button");  buttons.forEach(button => {   button.addEventListener("click", () => {     audio.play();   }); }); 
Enter fullscreen mode Exit fullscreen mode

Hopefully using the tutorial above you will know how to create Play Sound on Button Click.
Related Post:

  1. Responsive Footer HTML CSS
  2. International Schools in Bangalore
  3. Simple Stopwatch using JavaScript
  4. JavaScript Password Generator
  5. IB Schools in Hyderabad
  6. Sidebar Menu Using HTML CSS

I have previously created many JavaScript web elements for beginners. Please comment on how you like this onclick play audio button.

beginnershtmljavascriptwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.