Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9288423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:54:49+00:00 2026-06-18T19:54:49+00:00

Im trying to get a solution to create a new element and append it

  • 0

Im trying to get a solution to create a new element and append it around an empty element such like IMG, INPUT’s
i could easly just add a parent element around the IMG or INPUT, but i would rather do in via the DOM only.
The only way ive been able to do this so far is using cloneNode and appending it to the newly created element.

Example add a DIV via DOM around the already present IMG

Before:

<input type="text" />

After:

 <div><input type="text" /></div>

This is what i have created and works but because it deletes the node the defualt values get wiped i hope there is an simpler way to create and element and append around the INPUT

<input id="ic706" type="text" />

.

var e = document.getElementById("ic706")
var cloned = e.cloneNode(false);

e.parentNode.removeChild(e);

var c = document.createElement("div");
c.id = "container";

document.body.appendChild(c);
document.getElementById("container").appendChild(cloned);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:54:51+00:00Added an answer on June 18, 2026 at 7:54 pm

    I’d suggest:

    function wrapElem(el, wrapWith) {
        var newElem = document.createElement(wrapWith);
        el.parentNode.insertBefore(newElem, el);
        newElem.appendChild(el);
    }
    
    wrapElem(document.getElementsByTagName('input')[0], 'div');
    

    JS Fiddle demo.

    And seems to preserve event-binding.

    This works by creating a new element, inserting it before the element you want to wrap, and then moving the element to wrap into the wrapper, rather than deleting and recreating; so this should preserve any events, and properties, bound to the element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a new package in BIDS but i get the
I'm trying to get a simple solution for this layout. This is the simplified
I have a solution I'm trying to get to build on TFS. I want
i am trying to create a new flash movie. i am using CS4, for
Trying to create a new list item in and store the created item's ID
I am trying to make a 'Create New' page in MVC 4. The page
I'm new to ASP.NET MVC 3 and I'm trying to create a edit view
So new to objective-c and iphone/ipad development. Trying to get my feet wet with
I am trying to create a new object of type Invoice. I pass in
So i am trying get 2 div-containers which both should contain centered text (Both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.