jQuery addClass example

Change class name on click in jQuery

by crucify

HTML

<div id="banner-message">
  <div class="maxWidth">
    <div id="div1"></div>
    <div id="div2"></div>
  </div>
</div>

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;
}
#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 50%;
}
.maxWidth {
  display:inline-block;
}
#div1 {
  width: 50%;
  height: 20px;
  background-color: #FF0000;
}
#div2 {
  width: 200px;
  height:20px;
  background-color: #0000FF;
}

JavaScript

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

// handle click and add class
button.on("click", function(){
  banner.addClass("alt")
})