Auto-Enhanced document.createElement() — Method 1
Quick Start (30 seconds)
// Enable the enhancement once (or set it in config)
DOMHelpers.enableCreateElementEnhancement(); // Already enabled by default in the config,no need to rewrite it
// Now every document.createElement() call produces enhanced elements
const button = document.createElement('button');
// The .update() method is now available on the element
button.update({
textContent: 'Click Me',
classList: { add: ['btn', 'btn-primary'] },
style: { padding: '10px 20px' },
addEventListener: ['click', () => console.log('Clicked!')]
});
document.body.appendChild(button);What is Method 1?
Method 1 is the most familiar entry point into Dom Helpers element creation.
It does not ask you to change how you create elements. You still write document.createElement('div'). You still get back a real DOM element. The only difference is that the element now has an additional .update() method attached to it.
Think of it as a zero-migration upgrade. Your existing code does not need to change. You just gain new capability.
What is the .update() Method?
The .update() method is the heart of Dom Helpers. It accepts a configuration object and applies all the specified changes to the element at once.
Instead of writing:
element.textContent = 'Hello';
element.style.color = 'red';
element.classList.add('active');You write:
element.update({
textContent: 'Hello',
style: { color: 'red' },
classList: { add: ['active'] }
});Same result. One call. Everything in one place.
How to Enable Auto-Enhancement
There are two ways to enable it.
Option A: Enable Programmatically (Safest)
Call this once after including the library in your HTML:
DOMHelpers.enableCreateElementEnhancement();
// From this point forward, every document.createElement() call returns enhanced elements
const p = document.createElement('p');
p.update({ textContent: 'Enhanced!' }); // Works ✅When to use this: You are adding Dom Helpers to an existing project and want to be explicit about enabling enhancement. Call it once at the top of your main script.
Option B: Enable via Config (Always-On)
Inside the library source, there is a configuration object with a default setting:
const DEFAULTS = {
enableLogging: false,
enableWarnings: true,
autoEnhanceCreateElement: true, // ← Set this to true
};When autoEnhanceCreateElement is true, every document.createElement() call is automatically enhanced from the moment the library loads — no additional code needed.
When to use this: You are starting a fresh project with Dom Helpers from the beginning and want enhancement to be the default.
Syntax
// With programmatic enablement (Option A)
DOMHelpers.enableCreateElementEnhancement();
const element = document.createElement('tagName');
// With config set to true (Option B)
const element = document.createElement('tagName'); // Already enhanced
// Then use .update() on either
element.update({
// properties here
});Why Does This Exist?
When You Are Comfortable with the Native API
Some developers are comfortable with document.createElement() and prefer to keep using it. Method 1 respects that preference. You do not need to learn a new creation syntax — you just gain the .update() method on top of what you already know.
When You Are Adding Dom Helpers to an Existing Project
If your project already has code that uses document.createElement(), Method 1 lets you enhance that existing code without rewriting anything. Add the library, call enableCreateElementEnhancement(), and every element — old and new — gets .update().
The Design Reasoning
The auto-enhancement is opt-in by default for safety. If it were always-on automatically, it could interfere with edge cases in existing codebases — for example, code that checks whether an element has certain properties. By requiring a single explicit call to enable it, Dom Helpers ensures you are always in control.
Mental Model: The Invisible Upgrade
Think of Method 1 as an invisible upgrade to your toolbox.
Imagine you have been using a regular hammer for years. One day, someone swaps your hammer for one that looks identical, feels identical — but has a small laser level built into the handle. You pick it up and use it exactly the same way as before. But now you also have the laser level when you need it.
Method 1 is that hammer. document.createElement() works exactly as you expect. But now it also comes with .update() whenever you need it.
How Does It Work?
When you call DOMHelpers.enableCreateElementEnhancement(), the library intercepts the native document.createElement() function and wraps it.
Before enablement:
document.createElement('div')
↓
Browser creates a plain <div>
↓
Returns the plain element
After enablement:
document.createElement('div')
↓
Dom Helpers intercepts the call
↓
Browser creates the <div>
↓
Dom Helpers attaches .update() to the element
(using Object.defineProperty — non-enumerable, so it doesn't appear in loops)
↓
Returns the enhanced element ✨The .update() method is attached using Object.defineProperty, which means:
- It does not appear when you loop over the element's properties
- It does not interfere with any existing DOM APIs
- It cannot accidentally be overwritten
- It is specific to that element instance
Basic Usage
Creating a Simple Element
DOMHelpers.enableCreateElementEnhancement();
// Create a paragraph with .update()
const p = document.createElement('p');
p.update({
textContent: 'This paragraph was created with plain createElement!',
style: {
fontSize: '16px',
lineHeight: '1.6',
color: '#333'
}
});
document.body.appendChild(p);Applying Multiple Changes at Creation Time
const card = document.createElement('div');
card.update({
className: 'card',
id: 'featured-card',
style: {
padding: '20px',
background: 'white',
borderRadius: '8px',
boxShadow: '0 2px 4px rgba(0,0,0,0.1)'
}
});
document.body.appendChild(card);Adding Event Listeners with .update()
const button = document.createElement('button');
button.update({
textContent: 'Save Changes',
classList: { add: ['btn', 'btn-success'] },
style: {
padding: '10px 20px',
cursor: 'pointer'
},
addEventListener: ['click', (e) => {
console.log('Saving...');
e.target.update({
textContent: 'Saved!',
disabled: true
});
}]
});
document.body.appendChild(button);Notice something important here: inside the click handler, e.target.update() works too. When an event fires on an enhanced element, e.target also has the .update() method available.
Updating an Element After Creation
The .update() method is not just for configuration at creation time. You can call it at any point:
const statusDiv = document.createElement('div');
statusDiv.update({
textContent: 'Loading...',
style: { color: '#999' }
});
document.body.appendChild(statusDiv);
// Later, when data arrives
setTimeout(() => {
statusDiv.update({
textContent: 'Data loaded successfully!',
style: { color: '#28a745' }
});
}, 2000);classList Operations with .update()
const notification = document.createElement('div');
notification.update({
textContent: 'You have a new message',
classList: {
add: ['notification', 'notification--info'],
remove: ['hidden'],
toggle: 'pinned'
}
});
document.body.appendChild(notification);The classList key accepts an object where each property is a classList method (add, remove, toggle, replace). You can perform multiple class operations in one call.
Dataset with .update()
const item = document.createElement('li');
item.update({
textContent: 'Product Item',
dataset: {
productId: '42',
category: 'electronics',
inStock: 'true'
}
});
// Result: <li data-product-id="42" data-category="electronics" data-in-stock="true">
document.body.appendChild(item);setAttribute with .update()
const image = document.createElement('img');
image.update({
setAttribute: {
src: 'photo.jpg',
alt: 'A beautiful photo',
width: '400',
loading: 'lazy'
}
});
document.body.appendChild(image);Real-World Example: Dynamic Paragraph Counter
DOMHelpers.enableCreateElementEnhancement();
let counter = 0;
const container = Elements.container;
function addParagraph() {
counter++;
const p = document.createElement('p');
p.update({
id: `para-${counter}`,
textContent: `Paragraph #${counter}`,
classList: { add: ['dynamic'] },
style: {
padding: '10px',
margin: '5px 0',
background: '#f8f9fa',
borderLeft: '3px solid #007bff'
},
dataset: { index: String(counter) }
});
container.appendChild(p);
}
// Call this to add paragraphs dynamically
addParagraph();
addParagraph();
addParagraph();What .update() Can Accept
The .update() method handles a wide range of configurations:
| Key | What It Does | Example |
|---|---|---|
textContent | Sets the text content | textContent: 'Hello' |
innerHTML | Sets inner HTML | innerHTML: '<strong>Bold</strong>' |
id | Sets the element ID | id: 'myDiv' |
className | Sets the class string | className: 'card active' |
style | Applies style properties | style: { color: 'red' } |
classList | Adds/removes/toggles classes | classList: { add: ['active'] } |
setAttribute | Sets HTML attributes | setAttribute: { src: 'img.png' } |
removeAttribute | Removes HTML attributes | removeAttribute: 'disabled' |
dataset | Sets data attributes | dataset: { userId: '123' } |
addEventListener | Attaches event listeners | addEventListener: ['click', fn] |
disabled | Enables/disables element | disabled: true |
| Any DOM property | Sets that property directly | checked: true, value: '' |
Common Pitfall: Forgetting to Enable
// ❌ Wrong — calling .update() before enabling enhancement
const div = document.createElement('div');
div.update({ textContent: 'Hello' }); // Error: div.update is not a function
// ✅ Correct — enable enhancement first
DOMHelpers.enableCreateElementEnhancement();
const div = document.createElement('div');
div.update({ textContent: 'Hello' }); // Works!Simple rule: Call DOMHelpers.enableCreateElementEnhancement() once, at the top of your main script file, before creating any elements.
Common Pitfall: Cloned Elements Are Not Enhanced
When you clone an enhanced element, the clone does not automatically inherit enhancement:
DOMHelpers.enableCreateElementEnhancement();
const original = document.createElement('div');
// original.update is available ✅
const clone = original.cloneNode(true);
// clone.update is NOT available ❌
// cloneNode creates a plain copy — enhancement is not inheritedIf you need to use .update() on a cloned element, see Method 7 (Clone Existing Elements) in the Additional Patterns file.
Summary
Method 1 — Auto-Enhanced document.createElement() — is the most familiar and least disruptive way to start using Dom Helpers element creation.
Key points:
- ✅ You still write
document.createElement()exactly as before - ✅ Elements gain a
.update()method for applying multiple changes in one call - ✅ Enable once with
DOMHelpers.enableCreateElementEnhancement()or via config - ✅ Works with any existing code that uses
document.createElement() - ✅
e.target.update()also works inside event handlers
When to reach for Method 1:
- You are already comfortable with
document.createElement() - You are migrating an existing project to Dom Helpers
- You need a simple single element without a complex configuration
- You want maximum familiarity with the native API
What's Next?
- 04 — createElement with Config Object — Create a fully configured element in a single function call
- 05 — Bulk: Single Element — The declarative
createElement.bulk()approach