How to put a DIV tag around another tag in Javascript

1. I need to know how to put a div around a object on the page. I have <object></object> and I want it to be <div><object></object></div> from within javascript. It should work in IE and Moz and it should only put the div around the requested element.

Here is my javascript so far:

// get element on page. this element is in body tag with other elements after it
var myEl = document.getElementById("myElementID");

// create div to surround only the above element
var myDiv = document.createElement("div");
myDiv.style.position = "absolute";
myDiv.style.zIndex = 100;
// this works for IE and Mozilla
myDiv.style.left = 10+"px";
myDiv.style.top = 10+"px";
myDiv.style.height =400+"px";
myDiv.style.width = 300+"px";
myDiv.style.overflow = "auto";

// not sure what to do here...
omicronpersei8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
I think something like:

OBJcopy=myEl.cloneNode(true);
d=document.documentElement;
x=d.removeChild(myEl);
myDiv.appendChild(OBJcopy);

Cd&
0
ZvonkoSystems architectCommented:
Here an example:

<html>
<head>
<script>
function embedDiv(){
  // get element on page. this element is in body tag with other elements after it
  var myEl = document.getElementById("myElementID");
  // create div to surround only the above element
  var myDiv = document.createElement("div");
  myEl.parentNode.insertBefore(myDiv,myEl);
  myDiv.appendChild(myEl)
  alert(document.body.innerHTML)
}
</script>
</head>
<body>

<div id="myElementID">some div</div>

<p>
<a href="javascript:embedDiv()">Do it</a>
</p>
</body>
</html>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
superm401Commented:
That's the precise purpose of appendChild()
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GwynforWebCommented:
<script>
function addDiv(){
   document.getElementById('theDiv1').innerHTML='<div id="theDiv2" top="30" left="50">'+document.getElementById('theDiv1').innerHTML+'</div>'
}
</script>

<div id="theDiv1">
 <p>some div content</p>
</div>

<a href="#"onclick="addDiv();alert(document.getElementById('theDiv1').innerHTML);return false">Add div</a>
0
omicronpersei8Author Commented:
CobolD - i see that your answer could have worked had we had the chance to discuss it.
Zvonko - your solution worked. thank you. i appreciate the full example. it helps to show a full working example. cause then i know if something goes wrong in my project it's because i didn't implement it right.
superm401 - you're answer would have lead me to the final solution.
GwynforWeb - i appreciate that you provided another solution even though Zvonko provided a working solution. i like that you expressed your idea.
0
ZvonkoSystems architectCommented:
Thanks for the feedback omicronpersei8 .
0
superm401Commented:
I appreciate your generosity.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.