0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQueryでクリックした要素にclassを与えたり削除したり

Last updated at Posted at 2018-10-26

jQueryでclickした要素にclassを与えたり削除したり

ちょっとメモ書きに置いときます。

$('.day').on('click', function() {
    // クラスがあるか確認
    var selected = $(this).hasClass('selected');

    if (selected) {
        //クラス削除
        $(this).removeClass('selected');
    }
    else {
        // クラス追加
        $(this).addClass('selected');
    }
 });

toggleClassの方が楽だった

コメントで頂いた内容を試したら圧倒的に楽だった。

$('.day').on('click', function() {
    // 追加したり削除したり
    $(this).toggleClass('selected');
 });
0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?