Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

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;
  align-items: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!