Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App

HTML DOM insertAdjacentElement() Method

Last Updated : 13 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The insertAdjacentElement() method inserts the specified element at the specified position. The legal values for this position are.

  • afterbegin
  • afterend
  • beforebegin
  • beforeend

Syntax:

node.insertAdjacentElement(position, element)

Parameters: This method requires 2 parameters.

  • position: A position relative to the element. The legal values are :-
    1. afterbegin: Just inside the element, before its first child.
    2. afterend: After the element itself.
    3. beforebegin: Before the element itself.
    4. beforeend: Just inside the element, after its last child.
  • element: The element you want to insert.

Return Value: The element that was inserted, or null, if the insertion failed. 

Exceptions: If the specified position is not recognized or if the specified element is not a valid element. 

Example: 

HTML
<!DOCTYPE html> <html> <head>     <!--script to insert specified         element to the specified position-->     <script>         function insadjele() {             var s = document.getElementById("d1");             var h = document.getElementById("head3");                        h.insertAdjacentElement("afterend", s);         }     </script> </head>  <body>     <h1> Welcome To GeeksforGeeks</h1>     <div id="d1">div element</div>     <h3 id="head3">header element</h3>     <p>Click the button to insert        div element after the header element</p>     <button onclick="insadjele()">Insert element</button>  </body> </html> 

Output: 

Before clicking insert element button: 

 

After clicking insert element button: 

 

Supported Browsers: The browser supported by DOM insertAdjacentElement() Method are listed below:

  • Google Chrome 1 and above
  • Edge 17 and above
  • Firefox 48 and above
  • Opera 8 and above
  • Internet Explorer 5 and above
  • Safari 3 and above

P

ProgrammerAnvesh
Improve
Article Tags :
  • Web Technologies
  • HTML
  • HTML-DOM
  • HTML-Methods

Similar Reads

    HTML | DOM Style animationFillMode Property
    The DOM style animationFillMode property is used to specify the style of an element when the animation is not playing or when an animation is finished or when there is a delay in animation. The animationFillMode property can override the default behavior of CSS animations by which CSS animations aff
    3 min read
    HTML DOM ownerDocument Property
    The ownerDocument property returns the top-level document object of the node. Here, the owner document of the node is returned as the document object. It is a read-only property. Syntax: node.ownerDocument; Properties: nodeName property: It returns the name of the specified node. If the node is an e
    2 min read
    HTML | DOM Style transitionDelay Property
    The transitionDelay property in HTML DOM is used to specify the time in seconds or milliseconds when execution of transition starts. The delay refers to the time, which to be waited before starting the transition effect. Syntax: It returns the transitionDelay property.object.style.transitionDelayIt
    3 min read
    HTML DOM Style textDecorationStyle Property
    The Style textDecorationStyle property in HTML DOM is used to set the decoration of text with different kinds of lines. It can display the line in various styles like a single line, double line, wavy, etc. By using this property, we can display the line in a specified style. Syntax:   It returns the
    2 min read
    HTML | DOM Style listStyle Property
    The Style listStyle Property in HTML DOM used to set up to three list properties namely list-style-typelist-style-positionlist-style-image Syntax: It returns the listStyle property.object.style.listStyleIt used to set the listStyle property.object.style.listStyle = "type position image|initial|inher
    2 min read
    HTML | DOM Style outlineStyle Property
    The Style outlineStyle property in HTML DOM is used to set or return the style of the outline around an element. Syntax: It is used to return the outlineStyle property.object.style.outlineStyleIt is used to set the outlineStyle property.object.style.outlineStyle = value Property Values: none: This i
    2 min read
    HTML | DOM Heading Object
    The DOM Heading Object is used to represent the HTML <Heading> elements. The Heading elements is accessed by getElementById().Properties: align: This element contains a align attribute which is used to sets or returns the alignment of the heading elements. Syntax: document.getElementById("ID")
    1 min read
    HTML | DOM Ul Object
    The DOM Ul Object is used to represent the HTML <ul> element .the ul element is accessed by getElementById().Properties: compact: It is used to set or return whether the height of the unordered list would be render smaller than normal, or not. type: It is used to set or return the value of the
    2 min read
    HTML | DOM Time Object
    The DOM Time Object is used to represent the HTML Time element . The Time element is accessed by getElementById().Properties: dateTime: It contains single attribute datetime which is used to set or return the date/time in a machine-readable form of the element. Syntax: document.getElementById("ID");
    2 min read
    HTML DOM insertAdjacentHTML() Method
    The DOM insertAdjacentHTML() method is used to insert a text as HTML file to a specified position. This method is used to change or add text as HTML. Syntax : node.insertAdjacentHTML(specify-position, text-to-enter) Return Value : This will return the page with the specified change. There are four l
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences