1xptolevitico69

JAVASCRIPT Add/Remove class

Jan 20th, 2022 (edited)
1,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.27 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.   <meta charset="UTF-8">
  6.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.   <title>JAVASCRIPT Add/Remove class</title>
  9.   <style>
  10.  
  11.     .divDefault {
  12.       border: 5px solid red;
  13.       padding: 10px;
  14.       background-color: green;
  15.       color: snow;
  16.       display: inline-block;
  17.       font-size: 20px;
  18.       font-family: arial black;
  19.       cursor: context-menu;
  20.     }
  21.  
  22.     .divNewClass {
  23.       padding: 10px 50px;
  24.       background-color: red;
  25.       color: snow;
  26.       display: inline-block;
  27.       font-size: 50px;
  28.       font-family: arial black;
  29.       cursor: pointer;
  30.     }
  31.   </style>
  32. </head>
  33.  
  34. <body>
  35.  
  36.   <div onclick='changeDefault()' class="divDefault">I am a DIV element</div>
  37.  
  38.   <script>
  39.     x = document.getElementsByClassName("divDefault")[0];
  40.     toggle = true;
  41.  
  42.     function changeDefault() {
  43.       if (toggle) {
  44.         x.classList.add("divNewClass");
  45.         x.innerHTML = 'This is the new me!';
  46.         toggle = false;
  47.       } else if (toggle == false) {
  48.         x.classList.remove("divNewClass");
  49.         x.innerHTML = 'I am a DIV element';
  50.         toggle = true;
  51.       }
  52.     }
  53.   </script>
  54. </body>
  55.  
  56. </html>
Add Comment
Please, Sign In to add comment