Creating a div element in jQuery
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
jQuery provides several ways to create new DOM elements dynamically. The most common is passing an HTML string to the $() function, which parses it and returns a jQuery object you can manipulate before inserting it into the page. You can also pass a plain tag string with a properties object for a cleaner, XSS-safe approach. Understanding both methods helps you choose the right one based on whether your content comes from user input or is hardcoded.
Basic Creation
The created element exists in memory but is not yet part of the DOM — you must insert it explicitly.
Adding Attributes and Content
Using the Properties Object
The properties object accepts any jQuery method name as a key — text, html, css, click, addClass, attr, etc.
Inserting into the DOM
Creating Complex Structures
From an HTML String
Creating Multiple Elements
Performance: Document Fragment
For adding many elements, batch them to minimize DOM reflows:
Vanilla JavaScript Equivalent
Common Pitfalls
- XSS with
.html()and user input:$('<div>').html(userInput)executes any script tags in the input. Use.text()for untrusted content, which escapes HTML entities automatically. - Created elements are detached:
$('<div>')creates an element in memory. If you do not append/prepend/insert it into the DOM, it is never visible. Forgetting the insertion step is a common mistake. - Event handlers on dynamic elements: Events bound with
.on('click', handler)on a dynamically created element only work after the element is in the DOM. Alternatively, use event delegation:$(document).on('click', '.dynamic-class', handler). - Duplicate IDs: Creating multiple elements with the same
idattribute violates HTML spec and causes unpredictable behavior with$('#id')selectors. Use classes instead for repeated elements. - Performance with large HTML strings: Parsing large HTML strings with
$(htmlString)is slower than building withdocument.createElement. For thousands of elements, use document fragments or virtual DOM libraries.
Summary
- Use
$('<div>')or $('<div>', { properties })to create new elements - Chain
.attr(),.addClass(),.css(),.text(), and.on()to configure before inserting - Insert with
.append(),.prepend(),.after(),.before(), or.replaceWith() - Use
.text()instead of.html()for user-provided content to prevent XSS - Batch DOM insertions with document fragments for better performance
- Use event delegation for click handlers on dynamically created elements
Related reading
- Creating a new DOM element from an HTML string using built-in DOM methods or Prototype
- Creating an array consisting of the largest values of each sub-array does not work as expected
- CSS 100% height with padding/margin
- CSS Background Opacity
- CSS opacity only to background color, not the text on it?
- CSS selector for first element with class
- Custom calendar with event divs
- Custom navigation with Navigator component in React-Native
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.