Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Kristoffer A-L
Kristoffer A-L
5,863 Points

Change div when clicking a button

Hi! I'm building a Magic Eight ball, and I would like the ball, which I've designed in CSS, to change appearance when clicking a button. Is there a way to make this in JavaScript? To activate a transition from .ball to .ball-change? Thanks in advance.

.ball {
  margin: 0 auto;
  font-size: 3em;
  width: 20px;
  height: 20px;
  display: inline-flex;
  justify-content: center;
  padding: 25px;
  color: white;
  background-color: gray;
  border-radius: 100%;
  margin-bottom: 30px;
  border: 30px solid black;
  box-shadow: 3px 4px 5px #A9A9A9;
  transition: all 1s ease-out;

.ball-change {
  padding: 60px;
  background-color: blue;
  font-size: 5em;
  transition: all 1s ease-out;
  margin-bottom: 10px;

1 Answer

You could use element.classList.toggle(). Here's a link that may help explain it's use: https://www.w3schools.com/howto/howto_js_toggle_class.asp

Hope this helps!