-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASIC - DOM- change CSS rules.js
More file actions
94 lines (71 loc) · 2.12 KB
/
BASIC - DOM- change CSS rules.js
File metadata and controls
94 lines (71 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
!!BASIC - DOM - CHANGE CSS RULES!!
> getElementById - get single unique id element
> getElementsByClassName - get elements by class name
> getElementsByTagName - all elements that match the tag name
> When you’re dealing with the DOM it’s important to
execute your code only after the page is fully loaded
>> at the bottom of the body element!!!
*/
/*
CHANGE CSS
1) unique id
2) select it with CSS to style it
3) access this element through its id in JavaScript
*/
1)
<div id="menu">
...
</div>
2)
div#menu {
color: #aaa;
}
3)
var myMenu = document.getElementById("menu");
myMenu.style.color = "red"; /* OR -> "#FF0000"*/
/*
OR
1) set class & all its atributes
> change class $ its attributes
*/
1)
var planet = getElementById("greenplanet");
planet.setAttribute("class", "redtext");
/*
CSS - change many elements
*/
var myElements = document.getElementsByClassName(".bar");
for (var i = 0; i < myElements.length; i++) {
myElements[i].style.opacity = 0;
}
/*
CSS - add & remove classes
> get rid of query for now
1) To add the disableMenu class name to our dropDown element, use the add method on the
HTML element's classList property
2) To remove the the disableMenu class name, we can call the classList API's remove method
*/
1)
var theDropDown = document.getElementsByClassName("#dropDown");
theDropDown.classList.add("disableMenu");
2)
var theDropDown = document.getElementsByClassName("#dropDown");
theDropDown.classList.remove("disableMenu");
/*
1) WHAT IS THE VALUE OF AN ATTRIBUTE IN AN ELEMENT
2) if attribute doesn’t exist in the element, you get NULL (Test this)
*/
1)
var scoop = document.getElementById("raspberry");
var altText = scoop.getAttribute("alt");
console.log("I can't see the image in the console,");
console.log(" but I'm told it looks like: " + altText);
2)
var altText = scoop.getAttribute("alt");
if (altText == null) {
console.log("Oh, I guess there isn't an alt attribute.");
} else {
console.log("I can't see the image in the console,");
console.log(" but I'm told it looks like " + altText);
}