+ 7

[SOLVED] Manipulate CSS with Js

This is the code- https://code.sololearn.com/WBQGoJEHxS41/?ref=app Here, when I change color, the color of all boxes change into blue. But when I create a new box, it has color red again. I want to do something so that all the future boxes will also have color blue when the color button is clicked.

27th Feb 2021, 7:23 AM
Bartika🎶
Bartika🎶 - avatar
10 Respostas
+ 9
Easiest way is to go for css variables. Note: you might lose support for some old browser if you use css variables. https://code.sololearn.com/WFalswl204TO/?ref=app
27th Feb 2021, 8:44 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 11
maybe this will help instead of adding background in box class I made it in js create function and col(color) is a variable so its value changes to blue after changecolor is clicked ...https://code.sololearn.com/W7AqKlxn1p1n/?ref=app
27th Feb 2021, 7:44 AM
Atoms~⚛
Atoms~⚛ - avatar
+ 6
Calviղ, thanks for the answer. But I think if I do that and click the create button first before clicking the color button, then also all the boxes will turn into blue though i haven't clicked the color button yet. I hope you understand what i mean... this is not the actual code I'm working upon, but i just want to change the property from css stylesheet and not the style property of the document element(s) particularly.
27th Feb 2021, 7:30 AM
Bartika🎶
Bartika🎶 - avatar
+ 6
Bartika🎶 then just need to add conditional code
27th Feb 2021, 7:45 AM
Calviղ
Calviղ - avatar
+ 5
Run color() in create() function
27th Feb 2021, 7:26 AM
Calviղ
Calviղ - avatar
+ 4
i think you should create a loop that changes every second the boxes style depending on a variable which says if the boxes are red or blue
27th Feb 2021, 5:39 PM
Gabriele Giambrone
Gabriele Giambrone - avatar
+ 4
Add color() at the end of the create() function's code So when you click create button color() will run too and all the boxes shall be blue found better solution just add this code at the end of the create function's box.style.background ="blue" and it will change just the added box enjoy
28th Feb 2021, 6:25 AM
Atman Manou
Atman Manou - avatar
+ 2
Cc
28th Feb 2021, 3:38 AM
Ashwak
Ashwak - avatar
0
X=3 num=17 print(num%3)
1st Mar 2021, 4:14 AM
Maha Yahia
Maha Yahia - avatar