0

how do you change Css with Js without having "Style" found in Html?

For example, I dont want the styling in Html cuz this is what the lesson tells us when changing elements in Js (down below): –––––––––––––––––––––––––––– Html <body> <div id=a style="width=10px"> some text </div> <body> Css Js window.onload function() { var b = document .getElementById("a"); b.style.width = "20px"; ––––––––––––––––––––––––––––– But, I want Js to change styling in Css not in Html (down below): ––––––––––––––––––––––––––––– Html <body> <div id=a> some text </div> <body> Css #a { width:10px; } Js window.onload function() { var b = document .getElementById("a"); b.style.width = "20px"; ––––––––––––––––––––––––––––

22nd Aug 2018, 4:43 AM
Wilde Night
Wilde Night - avatar
7 Answers
+ 3
This is why you should share your codes on code playground here, we would understand more about your codes and give you solutions right away. Btw your id syntax is not working, making style function not working. It must be id="a" in html attribute. Lukas just showed you another way to work around it by creating a new class.
22nd Aug 2018, 5:40 AM
Calviղ
Calviղ - avatar
+ 1
Correction: id="a"
22nd Aug 2018, 5:06 AM
Calviղ
Calviղ - avatar
+ 1
I see
22nd Aug 2018, 5:33 AM
Calviղ
Calviղ - avatar
+ 1
yeah ik probably should have posted a link which is something similar to want i was requesting help with tho ive already anwsered my own question thx to reading the lession's comments but next time i will post a link of code for the future questions of mine
22nd Aug 2018, 5:46 AM
Wilde Night
Wilde Night - avatar
0
im working on something big (basically application ik big project for such a inexperiance coder ) and i would just like to see the basics on what im asking to work
22nd Aug 2018, 4:52 AM
Wilde Night
Wilde Night - avatar
0
actually you dont need to put the " around your id or class it still work without them
22nd Aug 2018, 5:14 AM
Wilde Night
Wilde Night - avatar
0
oh found a better solution to my question thx to Martinus Lukas whom i found in the comment of that lesson his suggestion to a solution was and i quote "I prefer just change class name, and separate class name in css file. // use example html in js file :: window.onload = function() { var x = document.getElementById("demo"); x.className = "demo2" }; and in css file :: .demo2 { color : ff30aa; width : 100px; }" ~Martinus Lukas
22nd Aug 2018, 5:37 AM
Wilde Night
Wilde Night - avatar