jQuery addClass example

Change class name on click in jQuery

by therichpost

HTML

<div id="banner-message">
  <p>Hello World</p>
  <button>Add Content!!</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;
  cursor:pointer;
}
.new_content{
  color:#fff;
  background: green; 
  color;border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;}
  .new_content:hover{background: #0084ff;}

JavaScript

// find elements
var button = $("button")
// handle click and add class
var a = 0;
button.on("click", function(){
	a++;
  $("#banner-message").append("<p class='new_content'   id='"+a+"'>new content</p>");
})