What am I doing wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What am I doing wrong?

Trying to make a toggle switch that moves left when true and right when false. How can I achieve this? https://code.sololearn.com/WYNAZpVJs42a/?ref=app

19th Jun 2018, 2:50 AM
Daniel Cooper
Daniel Cooper - avatar
8 Answers
+ 5
https://code.sololearn.com/W41W6P3A2U0z/?ref=app
19th Jun 2018, 3:28 AM
Calviղ
Calviղ - avatar
+ 3
You want the entire button to move, or do you want the colour to change by moving from left to right?
19th Jun 2018, 2:55 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Problem lies within your script. When you toggle the button, you set the innerHTML of the div, which overwrites your circle div, causing the circle to not display.
19th Jun 2018, 3:06 AM
Hatsy Rei
Hatsy Rei - avatar
+ 2
You could use getElementsByClassName[0] instead, it's equivalent.
19th Jun 2018, 3:34 AM
Calviղ
Calviղ - avatar
+ 1
I want to place a circle inside the red div that will move. But I can't get the circle to appear unless it's outside of the div.
19th Jun 2018, 2:57 AM
Daniel Cooper
Daniel Cooper - avatar
+ 1
lol. forgot about that. I can fix.
19th Jun 2018, 3:19 AM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Now now how can I animate it from left to right and vice versa?
19th Jun 2018, 3:27 AM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Why did you use query selector? Why not use getElementsByClassName? Sorry about all the questions new to coding xD
19th Jun 2018, 3:32 AM
Daniel Cooper
Daniel Cooper - avatar