LoginSignup
4
2

More than 5 years have passed since last update.

jQueryでイベントの順番を入れ替える

Posted at

プログラムを書いている中で、既存の登録済みのイベントの前に自分で実装したイベントを実行する必要があったので、そのやり方をメモとして書いてみた。
なるべくならこういうことがない方がいいけれど。

※注意

動作の保証は致しません。
参考程度に見て頂ければと思います。

書いたもの

HTML

<div>
  <button id="test" type="submit" name="button">ボタン</button>
</div>

JavaScript

// あらかじめ登録
$("button#test").on('click', function(){
  console.log("Hello");
});

// 入れ替えここから
var allEvents = $._data($('button#test').get(0), "events");
var clickHandler = allEvents.click[0].handler;
$("button#test").off('click', clickHandler);
$("button#test").on('click', function() {
  console.log("Hello World");
});
$("button#test").on('click', clickHandler);

結局は単純な入れ替えっぽくなった。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2