-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_select_html.js
58 lines (45 loc) · 2.28 KB
/
dom_select_html.js
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
// document.getElementById("heading1").innerHTML = 'this a head 2';
// document.querySelector('.pid').innerHTML = ' this is a peragraf';
function myMessage(){
var myVar = document.querySelector('#paraid1');
myVar.innerHTML = 'You have clicked on button1';
}
var heading3 = document.createElement('h2');
var text = document.createTextNode("this is a heading 2");
heading3.appendChild(text);
var myDiv = document.getElementById('my-dive');
myDiv.appendChild(heading3);
function addStyle(){
var myVer1 = document.querySelector('#paraid');
myVer1.classList.add('pera-style');
}
function removeStyle(){
var myVer1 = document.querySelector('#paraid');
myVer1.classList.remove('pera-style');
}
///DOM Selection Methods:
// getElementById()
// Retrieves an element by its id attribute.
let element = document.getElementById('elementId');
// getElementsByClassName()
// Retrieves a collection of elements with the specified class name.
let element1 = document.getElementsByClassName('className');
// getElementsByTagName()
// Retrieves a collection of elements with the specified tag name.
let element2 = document.getElementsByTagName('tagName');
// querySelector()
// Retrieves the first element that matches a specified CSS selector.
let element3 = document.querySelector('selector');
// querySelectorAll()
// Retrieves a list of elements that match a specified CSS selector.
let element4 = document.querySelectorAll('selector');
// getElementByName()
// Retrieves a collection of elements with the specified name attribute (mostly used for form elements)
let elements = document.getElementsByName('nameAttribute');
// Explanation:
// getElementById('id'): Returns the element with the specified id attribute.
// getElementsByClassName('class'): Returns a live HTMLCollection of elements with the specified class name.
// getElementsByTagName('tag'): Returns a live HTMLCollection of elements with the specified tag name.
// querySelector('selector'): Returns the first element that matches the specified CSS selector.
// querySelectorAll('selector'): Returns a static NodeList representing a list of elements that match the specified group of selectors.
// getElementByName('name'): Returns a NodeList or HTMLCollection of elements with the specified name attribute (commonly used with form elements).