2

I have a submit button, now I want result like this:

When I click on first time it will be run action 1.

When I click on second time it will be run action 2.

When I click on n time it will be run action n.

How can I do this?

Hong Van Vit
  • 2,672
  • 2
  • 13
  • 37

1 Answers1

6

Here is working solution i found Here.

$.fn.toggleClick = function(funcArray) {
  return this.click(function() {
    var elem = $(this);
    var index = elem.data('index') || 0;

    funcArray[index]();
    elem.data('index', (index + 1) % funcArray.length);
  });
};

$('.btn').toggleClick([
  function() {
    alert('From Function 1');
  }, function() {
    alert('From Function 2');
  }, function() {
    alert('From Function 3');
  }, function() {
    alert('From Function 4');
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button type="button" class="btn">Click Me</button>

This might help you.

Regards.

GNB
  • 2,158
  • 14
  • 27