jQuery addClass example

Change class name on click in jQuery

by roblugt

HTML

<div id="banner-message">Here are the results
<div id="x"></div>
<div id="y"></div>
<div id="z"></div>
<div id="xt"></div>
<div id="yt"></div>
<div id="zt"></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: 200px;
}

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

JavaScript

// find elements
var divX = $("#x")
var divY = $("#y")
var divZ = $("#z")
var divXT = $("#xt")
var divYT = $("#yt")
var divZT = $("#zt")

var o = {
x: null,
y: undefined,
z
}

divX.html((o.x == null) ? 'x is null' : ' x is not null');
divY.html(o.y == null ? 'y is null' : ' y is not null');
divZ.html(o.z == null ? 'z is null' : ' z is not null');

divXT.html(typeof o.x);
divYT.html(typeof o.y);
divZT.html(typeof o.z);