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 3 years have passed since last update.

JSでcssのクラスを追加・削除する方法(Jqueryを使わない)

Posted at

JavascriptでClassを追加・削除する方法を毎回忘れてしまうので、
備忘録として記載残しておきます。

.index.html
  <style>
    .addclass {
      color: red;
    }
  </style>
  <button id="addButton" onclick="addclass()">テスト文字です</button>
  <button onclick="delclass()">クラス削除</button>

  <script>
//#addButtonへクラスを追加します
    function addclass() {
      const buttonstyle = document.getElementById("addButton");
      buttonstyle.classList.add("addclass");
    }
//クラスを削除します
    function delclass() {
      const buttonstyle = document.getElementById("addButton");
      buttonstyle.classList.remove("addclass");
    }
  </script>

jQueryに慣れてしまうといざというときにできなかったりするのでメモしておきます。

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