Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-29-2012, 10:29 PM   PM User | #1
Null
New Coder

 
Join Date: Oct 2002
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
Null is an unknown quantity at this point
Post Question about the Cut & Paste HTML5 Mouseover/ Click sound effect script

Hi all,

I am using the following script posted on this site: http://www.javascriptkit.com/script/...oundlink.shtml

It works great, but I want to extend this script and mute the sound using a checkbox. This checkbox has the id: sound. If checked, I want to hear the sounds, when unchecked, no sounds should be heard.

This is the code I currently have for the checkbox:
Code:
function playSound() {
    if (document.getElementById('sound').checked){
        -something needs to be added here to make it work?-
    }
}
Anyone have an idea how to make this work?

Many thanks
Null is offline   Reply With Quote
Old 04-29-2012, 11:29 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
like so?

Code:
<body>
<audio id="aud" controls="controls">
  <source src="song.mp3" type="audio/mpeg" />
</audio>
<input type="checkbox" onclick="playSound(this)"/>
<script type="text/javascript">
function playSound(box) {
  document.getElementById('aud').muted = box.checked?true:false;
}
</script>
</body>
xelawho is offline   Reply With Quote
Old 04-29-2012, 11:38 PM   PM User | #3
Null
New Coder

 
Join Date: Oct 2002
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
Null is an unknown quantity at this point
No more like:
Code:
function playSound(playclip) {
    if (document.getElementById('sound').checked){
        document.getElementById(playclip).play();
    }
}
But using document.getElementById doesn't work, not sure if play clip is correct too. Please see the link provided in the first post for the code I use.

Thank you for your time
Null is offline   Reply With Quote
Old 04-30-2012, 12:40 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
I get it now...

Code:
<input type="checkbox" onclick="muteIt(this)"/>

function muteIt(box) {
if (clicksound){
  clicksound.muted = box.checked?true:false;
	}
 if (mouseoversound){
 mouseoversound.muted = box.checked?true:false;
	} 
}
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
Null (04-30-2012)
Old 04-30-2012, 12:50 AM   PM User | #5
Null
New Coder

 
Join Date: Oct 2002
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
Null is an unknown quantity at this point
Thank you for your reply. The problem with your code is that you need a click event with it (please correct me if I am wrong).

The checkbox can also be unchecked (it's checked/unchecked status is "remembered" in a cookie). This means that the click event is not always used (if someone unchecked the box and comes back to the site, the box will still be unchecked and no click is needed to mute it.

Thats why I used
Code:
if (document.getElementById('sound').checked){
So if checked play sounds, if not checked don't play them
Null is offline   Reply With Quote
Old 04-30-2012, 05:43 AM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
and you really can't change the code I gave you above to do that?
xelawho is offline   Reply With Quote
Old 04-30-2012, 11:47 AM   PM User | #7
Null
New Coder

 
Join Date: Oct 2002
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
Null is an unknown quantity at this point
Euhm no, I am just started learning js a bit using these examples.
Null is offline   Reply With Quote
Old 04-30-2012, 04:22 PM   PM User | #8
Null
New Coder

 
Join Date: Oct 2002
Posts: 28
Thanks: 1
Thanked 0 Times in 0 Posts
Null is an unknown quantity at this point
Nevermind, I solved it. I was thinking too hard, it can be done very simple:

Code:
    if (document.getElementById('sound').checked){
    				sound1.playclip(); 
    			}
This works
Null is offline   Reply With Quote
Reply

Bookmarks

Tags
audio, checkbox, html5, sound

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:10 PM.


Advertisement
Log in to turn off these ads.