0
2

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.

if ... else... 文 【JavaScript】

Last updated at Posted at 2020-09-03

##今回のポイント:point_up:

  • HTMLのチェックボックスにclickEventを設定

  • チェックがトグルと console.logにメッセージを表示

  • 加えて、body に対しclassList.toggleで背景色を付与

##【HTML】


   <body>
     <input type="checkbox" id="check">
     <script src ="main.js"></script>
   </body>

##【CSS】


.black {
  background: #333;
}

##【JavaScript】



   const checkBox = document.getElementById('check');
    
   checkBox.addEventListener('click', () => {
    document.body.classList.toggle('black');  // body に背景色を追加
    if (checkBox.checked) {
      console.log('チェックされたよ');
    } else {
      console.log('外れたよ');
    }
    });

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?