
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - DOM Element insertAdjacentHTML() Method
The HTML DOM Element insertAdjacentHTML() method is used to insert a specified HTML code (for eg. <p>Hello world</p>) at a specific position relative to the element that calls this method.
It allows you to add the HTML at various positions, such as: before being, end, after begin and, end of the specified element.
Syntax
Following is the syntax of the HTML DOM Element insertAdjacentHTML() method −
element.insertAdjacentHTML(position, html);
Parameters
This method accepts two parameters as listed below −
Parameter | Description |
---|---|
position | Specifies where to insert the HTML content relative to the element, beforebegin, afterbegin, beforeend, afterend. |
html | The HTML string to be inserted. |
Position Options:
- beforebegin: Places just before the specified element.
- afterbegin: Inserts right after the start of the specified element's content.
- beforeend: Inserts just before the end of the specified element's content.
- afterend: Adds immediately after the specified element.
Return Value
This method does not return any value.
Example 1: Inserting HTML Code afterend
The following program demonstrates the usage of the HTML DOM Element insertAdjacentHTML() method by inserting a paragraph (<p>) element after the end of the existing <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element insertAdjacentHTML</title> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } .new-content { background-color: #d6ffff; } </style> </head> <body> <h3>HTML DOM Element insertAdjacentHTML() Method</h3> <p>Inserts new HTML content 'afterend'.</p> <div id="container" class="container"> <h4>Existing content within this container.<h4> </div> <button onclick="insertAfterEnd()">Insert afterend</button> <script> function insertAfterEnd() { let container = document.getElementById('container'); container.insertAdjacentHTML('afterend', '<div class="new-content">New content-afterend</div>'); } </script> </body> </html>
The above program inserts an adjacent before the existing "div" element.
Example 2: Inserting HTML code beforeend
Following is another example of the insertAdjacentHTML() method. This method inserts a new <p> element with the text New content-beforeend just before the end of the existing <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element insertAdjacentHTML()</title> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } .new-content { background-color: #b3e0ff; } </style> </head> <body> <h3>HTML DOM Element insertAdjacentHTML() Method</h3> <p>Inserts new HTML content 'beforeend'.</p> <div id="container" class="container"> <h4>Existing content within this container.<h4> </div> <button onclick="insertBeforeEnd()">Insert beforeend</button> <script> function insertBeforeEnd() { let container = document.getElementById('container'); container.insertAdjacentHTML('beforeend', '<div class="new-content">New content-beforeend</div>' ); } </script> </body> </html>
When the button clicks the adjacent will be added before end of this existing "div".
Example 3: Inserting HTML code afterbegin
The following example shows the usage of the insertAdjacentHTML() method to insert a new <p> element with the text New content-afterbegin just after the beginning of the existing <div> element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element insertAdjacentHTML()</title> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } .new-content { background-color: #99ff99; } </style> </head> <body> <h3>HTML DOM Element insertAdjacentHTML() Method</h3> <p>Inserts new HTML content 'beforebegin'.</p> <div id="container" class="container"> <h4>Existing content within this container.<h4> </div> <button onclick="insertAfterBegin()">Insert afterbegin</button> <script> function insertAfterBegin() { let container = document.getElementById('container'); container.insertAdjacentHTML('afterbegin', '<div class="new-content">New content-afterbegin</div>' ); } </script> </body> </html>
The above program inserts a paragraph just after the end of the existing "div" element.
Example 4: Inserting HTML Code beforebegin
This example below inserts a new<p>element with the text New content-beforebegin just before the existing<div>element −
<!DOCTYPE html> <html lang="en"> <head> <title>HTML DOM Element insertAdjacentHTML</title> <style> .container { border: 1px solid #ccc; padding: 10px; margin: 10px; } .new-content { background-color: #ff8080; } </style> </head> <body> <h3>HTML DOM Element insertAdjacentHTML() Method</h3> <p>Inserts new HTML content 'beforebegin'.</p> <div id="container" class="container"> <h4>Existing content within this container.<h4> </div> <button onclick="insertBeforeBegin()">Insert beforebegin </button> <script> function insertBeforeBegin() { let container = document.getElementById('container'); container.insertAdjacentHTML('beforebegin', '<div class="new-content">New content-beforebegin</div>' ); } </script> </body> </html>
Supported Browsers
Method | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
insertAdjacentHTML() | Yes 5.0 | Yes 12.0 | Yes 8.0 | Yes 5.0 | Yes 11.50 |