-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetElement1.js
More file actions
48 lines (38 loc) · 1.4 KB
/
getElement1.js
File metadata and controls
48 lines (38 loc) · 1.4 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
// HTML DOM = Document Object Model
/**
* 3 main parts:
* elements: ID, Class, Tag, CSS selector, HTML collection
* attributes
* texts
*/
// JavaScript: Browser | Server (NodeJS)
// Browser: HTML -> DOM -> WEB API
// document.write("Hello");a
// HTML elements =/= HTML collections
// Get element methods
/**
* 1. getElementById
* 2. getElementsByClassName
* 3. getElementsByTagName
* 4. querySelector
* 5. querySelectorAll
*/
var headingNode = document.getElementById("heading"); // by class name of an element
console.log(headingNode); // object
console.log({
element: headingNode
});
var classNames = document.getElementsByClassName("classname"); // by class names
console.log(classNames);
var tags = document.getElementsByTagName("h1"); // by tag elements
console.log(tags);
var headingQuery = document.querySelector("h1");
console.log(headingQuery);
console.log(document.querySelector(".box .heading-2")); // or: html .box .heading-2 -> default first element
console.log(document.querySelector(".box .heading-2:first-child")); // first element
console.log(document.querySelector(".box .heading-2:nth-child(2)")); // second element
console.log(document.querySelector(".box .heading-2:nth-child(3)")); // third element
console.log(document.querySelectorAll(".box .heading-2")); // get all elements
console.log(document.forms);
console.log(document.forms["form-1"]);
console.log(document.anchors["youtube"]);