11

In the below code when the play button is clicked Its value should be changed to pause and when pause is clicked the other function should be called .How to do this using jquery toggle

  <input type="button" onclick ="play" value="play"/>
   <script>
     function play()
     {
               play_int();
           //   Button value to be changed to pause
     }   

  And when pause play_pause();
2
  • what you have tried till now??? Commented Apr 18, 2011 at 11:09
  • Don't use inline event handlers. With jQuery it does not really make sense also. Commented Apr 18, 2011 at 11:34

5 Answers 5

23

Give your button an ID:

<input type="button" id="play" value="play"/>

Then you can do something like this:

$('#play').click(function() {
   if ($(this).val() == "play") {
      $(this).val("pause");
      play_int();
   }
   else {
      $(this).val("play");
     play_pause();
   }
});

Or a slightly tidier version like this:

$(function(){
    $('#play').click(function() {
       // if the play button value is 'play', call the play function
       // otherwise call the pause function
       $(this).val() == "play" ? play_int() : play_pause();
    });
});

function play_int() {
    $('#play').val("pause");
    // do play
}

function play_pause() {
    $('#play').val("play");
    // do pause
}

Working demo

Sign up to request clarification or add additional context in comments.

Comments

14

try ( in jQuery < 1.9 )

$("input[type='button']").toggle(
    function(){
        $(this).val("Pause");
    }, 
    function(){
        $(this).val("Play");
    }
);

DEMO

Note: The toggle event was deprecated in 1.8 and removed in 1.9

3 Comments

Will this work with initial value ? I mean if the button is already set to Pause, will it toggle to Play ? I'm not sure.
@diEcho its not working in jQuery 1.9 and above versions. Is there any other way to do like this?
@LakshmanaKumar The toggle event was deprecated in 1.8 and removed in 1.9 read : forum.jquery.com/topic/…
2
$('#play_button_id').toggle(play,play_pause);

this will trigger the play() function when you click the button for the first time

and the play_pause() when you click the button for the second time

1 Comment

No, it won't. Unless these functions return with another function :). You should use toggle(play, play_pause).
2

I would say

statuses = ['Play', 'Pause'];
$('#btn_play').val(
    $('#btn_play').val() == statuses[0]
    ? statuses[1] : statuses[0]
);

Comments

1

A simplest idea can be this

function play(action) {
 { 
   if(action=='play') { 
      play_int(); 
      $("#btn_play").val('pause');
   }
   else { 
      pause_int(); 
      $("#btn_play").val('play');
   }
}

$("#btn_play").click(function() {
   val = $(this).val();
   play(val);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.