Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JAVASCRIPT Add/Remove class</title>
- <style>
- .divDefault {
- border: 5px solid red;
- padding: 10px;
- background-color: green;
- color: snow;
- display: inline-block;
- font-size: 20px;
- font-family: arial black;
- cursor: context-menu;
- }
- .divNewClass {
- padding: 10px 50px;
- background-color: red;
- color: snow;
- display: inline-block;
- font-size: 50px;
- font-family: arial black;
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <div onclick='changeDefault()' class="divDefault">I am a DIV element</div>
- <script>
- x = document.getElementsByClassName("divDefault")[0];
- toggle = true;
- function changeDefault() {
- if (toggle) {
- x.classList.add("divNewClass");
- x.innerHTML = 'This is the new me!';
- toggle = false;
- } else if (toggle == false) {
- x.classList.remove("divNewClass");
- x.innerHTML = 'I am a DIV element';
- toggle = true;
- }
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment