Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Down below is how i have the hyperlink and image set up.

so when you click the image i want it to make the "click" sound which i have and called it "click1.mp3"

I just want this as simple as possible as its just going to be used in a local network via machines that all will be using chrome browsers.

I've looked everywhere and while i have found various methods to make the image play the click sound when pressed, the second i add the hyperlink, clicking the image takes me to where i want it to go, but it doesn't make the click sound upon pressing it. just been frustrated already , something that should be so simple.

anybody have any ideas?

thanks in advance

What I have tried:

HTML
<a href="www.wherever.com"></a>
Posted
Updated 6-Jun-18 4:32am
v2

Clicking on a hyperlink will cause the browser to immediately navigate to the linked page. The audio on your page won't have time to start playing.

You'll need to delay the navigation until the audio has finished playing:
JavaScript
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++){
    links[i].addEventListener("click", function(event){
        event.preventDefault();
        
        var url = this.href;
        var audio = document.getElementById("myAudio");
        audio.play().then(function(){
            audio.addEventListener("ended", function(){
                location.href = url;
            });
        });
    });
}

Updated demo[^]
 
Share this answer
 
Here is an example.

cp_image_click_sound - JSFiddle[^]
 
Share this answer
 
Comments
Richard Deeming 6-Jun-18 10:22am    
None of those examples includes a hyperlink. :)
Bryian Tan 7-Jun-18 7:03am    
You’re right :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900