Cybernetic
Cybernetic

Reputation: 13334

Only open accordion once, but still call onclick each time, when button clicked in jquery

I am using jquery accordion animation:

$('.accordion').click(function() {
       $(this).next().toggle("fold", 1000);
       return false;
  }).next().hide();

along with a button for expanding the div below:

<button class="accordion" onclick="my_method()">EXPAND</button>
    <div  class="panel">
        ...contents to expand here
    </div>

This works great. But I only want to allow the accordion to open once (unless page refreshed obviously), although the onclick event should call the method every time the button is clicked.

Upvotes: 2

Views: 1428

Answers (1)

max
max

Reputation: 8667

You can use one() method.

$('.accordion').one('click', function() {
  $(this).next().toggle("fold", 1000);
  return false;
}).next().hide();

CODEPEN

Upvotes: 4

Related Questions