- Meta description: Add a click sound to a button with a few lines of JavaScript. Two tested snippets, the browser autoplay rule and how to pick a click that is short enough.
- Slug: add-button-click-sound-website
- Target query: add button click sound to website
To add a click sound to a button, load a short audio file and play it from the button's click handler. Five lines of JavaScript do it. The rest of this guide covers what those five lines miss: fast repeated clicks, many buttons, browser autoplay rules and choosing a sound that stays pleasant after the hundredth click.
The shortest version#
<button id="buy">Add to cart</button>
<script>
const clickSound = new Audio('click.wav');
clickSound.preload = 'auto';
document.getElementById('buy').addEventListener('click', () => {
clickSound.currentTime = 0;
clickSound.play().catch(() => {});
});
</script>click.wav is your own file. currentTime = 0 rewinds the sound so a second click restarts it instead of being ignored. play() returns a promise, and the empty catch keeps a blocked or interrupted play from filling the console with errors.
This is enough for one or two buttons. Its limit is that one audio element plays one sound at a time: a fast second click cuts the first one off.
A version for a whole interface#
For a toolbar, a form or an app, decode the sound once with the Web Audio API and start a new lightweight source on every click. Overlapping clicks then layer instead of cutting each other off, and one gain node sets the volume for all of them.
<button data-click-sound>Add to cart</button>
<button data-click-sound>Save</button>
<script>
const ctx = new AudioContext();
const gain = ctx.createGain();
gain.gain.value = 0.4; // keep UI sounds quiet
gain.connect(ctx.destination);
let clickBuffer = null;
fetch('click.wav')
.then((res) => res.arrayBuffer())
.then((data) => ctx.decodeAudioData(data))
.then((decoded) => { clickBuffer = decoded; });
let muted = localStorage.getItem('ui-sound') === 'off';
function playClick() {
if (muted || !clickBuffer) return;
if (ctx.state === 'suspended') ctx.resume();
const source = ctx.createBufferSource();
source.buffer = clickBuffer;
source.connect(gain);
source.start();
}
document.addEventListener('click', (event) => {
if (event.target.closest('[data-click-sound]')) playClick();
});
</script>Any element with a data-click-sound attribute now makes the sound, including buttons you add later, because the listener sits on the document. To let people turn the sound off, set localStorage.setItem('ui-sound', 'off') from a toggle in your settings and read it back on load, as the snippet does.
Why the sound plays only after a click#
Browsers block audio that a page starts on its own. MDN describes it this way: playback "is generally blocked if the playback is programmatically initiated in a tab which has not yet had any user interaction." The same page says a site can assume audio will play once the user has interacted with it by "clicking, tapping, pressing keys, etc.", and that starting a Web Audio source outside a user input event is subject to the same rules (MDN: Autoplay guide).
A click sound is triggered by a click, so it is allowed. Keep it that way: play UI sounds from click, tap or key handlers, never on page load. The ctx.resume() line in the second snippet covers the case where the audio context was created before the first interaction and is still suspended.
Pick a click that fits the button#
Length matters more than character. A click that ends in a tenth of a second reads as the button's own feedback. One that rings longer starts to sound like a notification.
The button click sound effects collection has short clicks you can preview before you pick one. Three examples, with the lengths stated on their pages:
- Button Click 01 is 0.05 s, for a primary button or a card tap.
- Soft Click is 0.10 s and warm, for buttons and list rows.
- Crisp Click is 0.09 s and bright, for a primary button, a checkbox or a keyboard shortcut.
Use one family of sounds across the interface and give different jobs different clicks: a quiet one for list rows, a firmer one for the main action. The wider UI sounds library has more sounds for other interface actions.
Each sound page states its licence. The three above are CC0 1.0 (public domain, from Kenney's packs) and come as WAV and MP3, so commercial use needs no credit. Downloads are included with a HorizonX membership.
Keep the sound optional#
Do not let sound carry the message alone. The button also needs a visible pressed or loading state, because many people browse with the sound off.
WCAG success criterion 1.4.2 asks for a way to pause or control audio that plays automatically for more than 3 seconds. A short sound that follows a click falls outside that rule, since the W3C notes that audio started by a clear user action is outside the intent of the criterion (W3C: Understanding 1.4.2). A mute switch is still worth adding, and the second snippet shows the minimum.
How these snippets were checked#
I ran both snippets in Chromium with a generated 60 ms test click, using a real click from browser automation. In the first, play() resolved and the sound played once to the end. In the second, the audio context was running after the first click, the sound decoded to 0.06 s, and three clicks on data-click-sound buttons started three sources, while a button without the attribute started none. The test reads playback state, so it does not prove how the sound feels through speakers. Safari, Firefox and iOS were not tested, so try the snippets on a real phone before you ship.
FAQ#
Why does a button click sound fail to play on the first load?#
Browsers block programmatic audio until a user interacts with the page. Playing the sound inside a click, tap, or keydown handler satisfies the autoplay rule, and calling ctx.resume() wakes a suspended Web Audio context.
Can I use HorizonX UI click sounds in commercial projects?#
Yes. HorizonX sound packs such as Button Click 01, Soft Click, and Crisp Click carry a CC0 1.0 public domain license (via Kenney) and include WAV and MP3 files, so commercial use requires no attribution.
Next step#
Preview the clicks in your browser, then pick one for your primary button: Browse the button click sound effects.