jQuery addClass example
Change class name on click in jQuery
by Vasilii Chugunov
HTML
<div id="banner-message">
<p>Hello World</p>
<button id="first">Change color</button>
<button id="second">Другая кнопка</button>
<button id="thild">Третья кнопка</button>
</div>
CSS
.big {
font-size: 64px !important;
}
.smoll {
background:red !important;
font-size: 7px !important;
}
#banner-message.alt {
background: #0084ff;
color: #000000;
margin-top: 160px;
width: 200px;
}
#banner-message.alt button {
background: yellow;
color: #000;
}
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;
}
JavaScript
// find elements
var banner = $("#banner-message")
var button1 = $("#first")
var button2 = $("#second")
var button3 = $("#thild")
button2.on("click", function() {
banner.toggleClass("big")
})
// handle click and add class
button1.on("click", function() {
banner.toggleClass("alt")
})
button3.on("click", function() {
banner.toggleClass("smoll" )
})