/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] toggle control button

Hello Everyone… its been a long time since ive done a bit of the old javascript and im having trouble getting something like this to work:

I have a media player that I can send control functions too: play; pause; stop etc… but I can only do it with seperate buttons for play and pause (as below)… it all works as is…

What I would like is a toggle button for play/pause that changes depending on the state of the media clip. If paused it shows “play” and vice-versa.

Can anyone help?

[CODE]<head>
function play(){
make object play
}

function pause(){
make object pause
}
</head>
<body>

<!– two seperate buttons need to make into one toggle button –>
<input type=”button” onClick=”play()” value=”play”>
<input type=”button” onClick=”pause()” value=”pause”>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Sirin86May 08.2008 — Just use a boolean:
<i>
</i>&lt;script&gt;
var playing = false;

function toggle(){
if (playing){
make element pause
}else{
make element play
}
document.getElementById("playBtn").value = (playing ? "Play" : "Pause");
playing = !playing;
}
&lt;/script&gt;
&lt;input type = "button" onclick = "toggle" value = "Play" id = "playBtn" /&gt;
Copy linkTweet thisAlerts:
@mrhooMay 08.2008 — function playpause(e){
var e= window.event || e;
var who= e.target || e.srcElement;
var val= who.value;

<i> </i>if(val== 'Play'){
<i> </i> who.value= 'Pause';
<i> </i> make object play
<i> </i>}
<i> </i>else{
<i> </i> who.value= 'Play';
<i> </i> make object pause
<i> </i>}
}

<input type="button" onClick="playpause()" value="Play">
Copy linkTweet thisAlerts:
@aerrisauthorMay 08.2008 — Thanks Sirin

your solution worked a treat!

[CODE]<script>
var playing = false;

function toggle(){
if (playing){
alert("now im paused");
}else{
alert("now im playing");
}
document.getElementById("playBtn").value = (playing ? "Play" : "Pause");
playing = !playing;
}
</script>
</head>
<body>
<input type = "button" onclick = "toggle()" value = "Play" id = "playBtn" />[/CODE]


thanks also mrhoo but the first one sorted me out ?
×

Success!

Help @aerris spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...