Manipulate your HTML/CSS using Javascript
The M
What's DOM
DOM Methods and Props
DOM Strucutre
Steps to creating an HTML element
+ Deleting
Steps to Modify HTML/CSS
Event Listeners
in DOM
Dumb Old Monkey?
Don't Offer Me more work?
Dogs on Mars?
Daulphin Olympics Marathon?
Dangerous Overpriced Meals?
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
DOM Method
Property
Note: DOM elements are manipulated as objects
Parent object: document
//Finding HTML elements by id
const element = document.getElementById("full_stack");
//Finding HTML elements by tag name
const element = document.getElementsByTagName("p");
//Finding HTML elements by class name
const x = document.getElementsByClassName("green");
//Finding HTML elements by CSS selectors
const x = document.querySelectorAll("p.intro");
//innerHTML
document.getElementById('index1').innerHTML = 'You can write whatever here, even <br> HTML tags';
//innerText
document.getElementById('index1').innerText = 'You can also write text here, but no html tags.';
//value
const input_value = document.getElementById('input').value;
//style
document.getElementTagName('div').style.color = 'blue';
//className
document.getElementTagName('div').className = 'class1 class2';
// GENERALY WE USE:
paragraph.setAttribute('class', 'class1')
// We're trying to add a paragraph inside a div with the id: index
const div = document.findElementById('index');
//or
const div = document.querySelector('div');
//Create the paragraph element
const paragraph = document.createElement('p');
paragraph.textContent = 'fullstack js training';
//Create the paragraph element
div.appendChild(paragraph);
//Delete child
div.removeChild(paragraph);
////Delete an element specifically
const paragraph = document.findElementTagName('p');
paragraph.remove();
- Delete a child of an element
- Delete the element you're on
// element.addEventListener(event, function, useCapture);
document.getElementById("myBtn").addEventListener("click", displayDate);
document.getElementById("myBtn").addEventListener("mouseover", myFunction);
document.getElementById("myBtn").addEventListener("click", mySecondFunction);
document.getElementById("myBtn").addEventListener("mouseout", myThirdFunction);
Event Listener: Yestenak to make the action, then executes a certain function
// To end the event listener
element.removeEventListener("mousemove", myFunction);