Last updated: Mar 5, 2024
Reading timeยท3 min
To create an element with an onClick event listener:
document.createElement()
method to create the element.addEventListener()
method to add a click
event listener to the
element.appendChild()
method.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div id="box"></div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
// โ Create element const el = document.createElement('div'); el.addEventListener('click', function handleClick(event) { console.log('element clicked ๐๐๐', event); }); // โ Add text content to the element el.textContent = 'Hello world'; // โ Or set the innerHTML of the element // el.innerHTML = `<span>Hello world</span>`; el.style.backgroundColor = 'salmon'; el.style.width = '150px'; el.style.height = '150px'; // โ add element to DOM const box = document.getElementById('box'); box.appendChild(el);
We used the document.createElement() method to create the element.
const el = document.createElement('div');
div
in the example).The createElement
method returns the newly created element.
We called the addEventListener() method on the element, passing it the following 2 parameters:
click
in the example).event
is triggered.const el = document.createElement('div'); el.addEventListener('click', function handleClick(event) { console.log('element clicked ๐๐๐', event); });
handleClick
function is invoked.You can use the textContent property to set the element's text content or the innerHTML property to set the element's inner HTML markup.
// โ Create element const el = document.createElement('div'); el.addEventListener('click', function handleClick(event) { console.log('element clicked ๐๐๐', event); }); // โ Add text content to the element el.textContent = 'bobby hadz tutorial'; // โ Or set the innerHTML of the element // el.innerHTML = `<span>Hello world</span>`;
innerHTML
property with user-provided data without escaping it. This would leave your application open to cross-site scripting attacks.You can use the appendChild() method to add the element to the page.
// โ Create element const el = document.createElement('div'); el.addEventListener('click', function handleClick(event) { console.log('element clicked ๐๐๐', event); }); // โ Add text content to the element el.textContent = 'Hello world'; // โ add element to DOM const box = document.getElementById('box'); box.appendChild(el);
The method adds a node to the end of the list of children of the element it was called on.
You can also use the onclick
attribute to add an event lister after creating
the element.
// โ Create element const el = document.createElement('div'); el.onclick = function handleClick(event) { console.log('element clicked ๐๐๐', event); }; // โ Add text content to the element el.textContent = 'bobby hadz tutorial'; // โ Or set the innerHTML of the element // el.innerHTML = `<span>Hello world</span>`; el.style.backgroundColor = 'salmon'; el.style.width = '150px'; el.style.height = '150px'; // โ add element to DOM const box = document.getElementById('box'); box.appendChild(el);
The onclick attribute specifies a function to run when the element is clicked.
Either approach works, but the addEventListener
method is more commonly used.
You can learn more about the related topics by checking out the following tutorials: