Create An Element With OnClick Event Listener Using JS | Bobbyhadz
Maybe your like
# Create an element with onClick event listener using JS
To create an element with an onClick event listener:
- Use the document.createElement() method to create the element.
- Use the addEventListener() method to add a click event listener to the element.
- Add the element to the page using the appendChild() method.
Here is the HTML for the examples.
index.htmlCopied!<!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> The code for this article is available on GitHubAnd here is the related JavaScript code.
index.jsCopied!// ✅ 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.
index.jsCopied!const el = document.createElement('div'); The only parameter we passed to the method is the type of element to be created (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:
- The event type to listen for (click in the example).
- A function that gets invoked when the event is triggered.
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.
index.jsCopied!// ✅ 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>`; The code for this article is available on GitHubYou shouldn't use the 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.
index.jsCopied!// ✅ 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 code for this article is available on GitHubThe method adds a node to the end of the list of children of the element it was called on.
# Create an element with onClick event listener using onclick
You can also use the onclick attribute to add an event lister after creating the element.
index.htmlCopied!// ✅ 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 code for this article is available on GitHubThe onclick attribute specifies a function to run when the element is clicked.
Either approach works, but the addEventListener method is more commonly used.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
- Create an Element with Attributes or Styles in JavaScript
- Create an Element with Classes or ID in JavaScript
- How to Create an Image Element using JavaScript
- Create a Script element using JavaScript
- How to create a style tag using JavaScript
- How to Create a Video element using JavaScript
- onclick not working in JavaScript or React.js [Solved]
Tag » Add Div On Click Javascript
-
Add Click Event On Div Tag Using JavaScript - Html - Stack Overflow
-
How To Add A Class Name - W3Schools
-
HTML DOM Document CreateElement() Method - W3Schools
-
JQuery Append() Method - W3Schools
-
HTML DOM Element Click() Method - W3Schools
-
How To Append HTML Code To A Div Using JavaScript - GeeksforGeeks
-
How To Add A Number To A Div After Clicking A Button In JavaScript
-
How To Add A Click Event Listener On Div Tag Using JavaScript?
-
Onclick Append Class To Div In Javascript Code Example
-
How To Append Or Add Text To A DIV Element Using JavaScript
-
JavaScript – Insert Element After Specific Div Element - Tutorial Kart
-
Add New Div While Click A Button Using Javascript Or Jquery
-
Javascript Add Button To Div Code Example
-
HTML Button Onclick – JavaScript Click Event Tutorial