jQuery addClass example

Change class name on click in jQuery

by therichpost

HTML

<div id="banner-message">
  <button>
  click me!!
  </button>
</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;
}

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

JavaScript

// append button
$("button").click(function(){
	$("#banner-message").append("<div id='button-container'><button>click me!!</button></div>");
});

$("#banner-message").on('click', '#button-container button', function(){
 alert("Click Works!!");
});