document.getElementById()
, querySelector()
createElement()
, appendChild()
, innerHTML
Node
, Element
, Text
, etc.<!-- index.html -->
<body>
<div id="container"></div>
<script>
// Select an existing element by its ID
const container = document.querySelector('#container');
// Create a new <p> element
const paragraph = document.createElement('p');
paragraph.textContent = "Hello, DOM!";
// Append the paragraph to the container
container.appendChild(paragraph);
</script>
</body>
createTextNode
In a browser, the document
object is a global object that represents the DOM. window
and document
are global objects and can be accessed directly.
window
in the Browserwindow
is the global object that represents the browser window.document
, localStorage
, fetch
, and more.window
directly in a browserjavascript
CopyEdit
console.log(window); // Logs the global `window` object
console.log(window.innerWidth); // Logs the browser's viewport width
console.log(window.location.href); // Logs the current URL
Since window
is global, you can omit window.
and just write:
javascript
CopyEdit
console.log(location.href); // Equivalent to `window.location.href`
document
in the Browserdocument
is a property of window
(window.document
).