All right. 1
So now that we know how to select elements using the DOM, it's time to learn how to use Javascript to 2
manipulate the elements that we've selected. 3
Now, as you saw earlier on, we were able to change the CSS style dynamically using Javascript by tapping 4
into the style property of an element object. 5
So, for example, we could say document.querySelector(“h1”).style.color = “red”. 6
And that selects on the h1 element in our document, changes the style color to a red color. 7
Now every single CSS property can be changed in this way using Javascript, but the property names might 8
look a little bit different. 9
As we said earlier on, Javascript naming conventions tend to be camel cased. 10
So, for example querySelector, where the first word is lowercase 11
and the second word begins with an uppercase letter. 12
This is camel casing and this is standard for naming methods and properties in Javascript. 13
So that means that when you're trying to change the CSS styles, you will find that the property names 14
don't look quite the same as what you see in CSS. 15
So, for example, we might have things like font size, which is font-size as the property name in CSS, 16
but if you're trying to change it using Javascript, then the property is going to be camel cased like 17
so. 18
So let's change the font size to, I don’t know, something massive, 10rem. And you can find all of those names inside 19
the DOM style object documentation and you'll see all of these properties and what they look like when 20
you're trying to change it using Javascript instead of CSS. 21
And I'll leave a link to this in the resources of this current lesson so you can check it out. 22
But, as a rule of thumb, in most cases they're exactly the same, 23
other than the fact that there are no dashes and every word after the first one will have its first 24
letter capitalized. 25
The other thing to notice is that the values that you're going to set it to have to be represented as 26
strings. Whereas in CSS we simply just specified the value as is, for example padding 7 percent, if we're 27
trying to do this with Javascript, then we actually have to specify everything as a string, even if it's 28
a number, like line height or padding. 29
So, just so you can see more clearly, I'm inside the Elements tab of the Chrome Developer Tools, 30
and as you might remember, you can quite easily select an item over here and change its CSS by just typing 31
it inside here, 32
so say visibility is hidden, and that hides our h1. 33
But if we wanted to do the same thing using Javascript, then we would have to specify that value “hidden” 34
as a string. 35
So remember those two differences. 36
Now it's your turn to try it out. 37
I want you to use Javascript only to change the background color of our button here to a yellow color. 38
And you might have to take a look at the object style properties or you can just give it a go and see 39
if you can predict what it might be. 40
So pause the video now and give that challenge a go. 41
All right. 42
So you might already realize that in order to change the background color of our button, then in CSS you 43
would simply just tap into the background color property and you would set it to yellow. 44
And that would achieve what we wanted. 45
But because we have to do it using Javascript, then we need to know the Javascript version of that background 46
color property. 47
So if we head over to our Style Object Properties documentation and we hit command f and we search for background 48
color, then you can see that it brings up the property 49
that’s called backgroundColor. 50
And as some of you might have predicted it's simply the same property without the dash and camel case. 51
So let's now use it inside our console. 52
So we're going to tap into the document and query for the selector 53
that is a button, and change its style property, namely the background color property, to, remember the value 54
has to be specified as a string, 55
so it's the word yellow 56
inside some quotation marks. 57
Now if we hit enter, our Javascript code has turned the background color of the button yellow. 58
So if you found that difficult at all, 59
then I recommend going through some of the other properties that we've been using in CSS and trying 60
to do it using Javascript instead, 61
and have a play around with the Chrome Developer Tools until you really get it. 62
Now in the next lesson we're going to learn all about the separation of concerns and how we can keep 63
our style inside the CSS but toggle it on and off using Javascript. 64
So for all of that and more, I'll see you on the next lesson.