domcontentloaded event listener code example
Example 1: listerner content loaded js
window.addEventListener('DOMContentLoaded', (event) => {
console.log('DOM fully loaded and parsed');
});
Example 2: dom is loaded
document.addEventListener('DOMContentLoaded', (event) => {
console.log('DOM fully loaded and parsed');
});
Example 3: domcontentload event in javascript
<!DOCTYPE html>
<html>
<head>
<title>JS DOMContentLoaded Event</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
let btn = document.getElementById('btn');
btn.addEventListener('click', () => {
console.log('clicked');
});
});
</script>
</head>
<body>
<button id="btn">Click Me!</button>
</body>
</html>
Code language: HTML, XML (xml)