jQuery toggleClass example

Toggle class name on click in jQuery

by nikolya223

HTML

<style>
          #table_year {
            table-layout: fixed;
            width: 100%;
            border: 1px solid hsl(0, 0%, 66%);
            border-radius: 4px;
          }
 
          .td_month {
            width: 260px;
            height: 270px;
            border: 1px solid hsl(0, 0%, 66%);
            border-radius: 4px;
          }
 
          #num_year,
          #January,
          #February, 
          #March,
          #April, 
          #May,
          #June, 
          #July,
          #August, 
          #September,
          #October, 
          #November,
          #December {
            background-color: hsl(210, 100%, 95%);
            color: hsl(210, 100%, 50%);
            font: bold 18px serif;
            text-align: center;
            border: 1px solid hsl(0,0%,66%);
            border-radius: 4px;
            padding: 5px;
            margin: 0 0 5px 0;
          }
 
          #num_year {
            font: bold 24px serif;
            width: 780px;
            border: 1px solid hsl(0, 0%, 66%);
            border-radius: 4px;
            padding: 5px;
            margin: 0 0 5px 0;
          }
 
          #month1,
          #month2,
          #month3,
          #month4,
          #month5,
          #month6,
          #month7,
          #month8,
          #month9,
          #month10,
          #month11,
          #month12 {
            display: inline-block;
            border: 1px solid hsl(0, 0%, 66%);
            border-radius: 4px;
            padding: 5px;
            width: auto;
          }
 
          .day {
            border: 1px solid hsl(0, 0%, 66%);
            width: auto;
            line-height: 30px;
            text-align: center;
            margin: 1px;
            background-color: hsl(210, 100%, 95%);
            color: hsl(210, 100%, 50%);
          }
 
          .table_month {
            table-layout: fixed;
            width: 100%;
            height: 220px;
            border: 1px solid hsl(0, 0%, 66%);
...

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}

JavaScript

// find elements
var banner = $("#banner-message")
var button = $("button")

// handle click and add class
button.on("click", () => {
  banner.toggleClass("alt")
})