views:

109

answers:

4

Hello,

I have a series of div tags on my page & I want to wrap them all into a container.

<div class = "c" >blah blah </div>
<div class = "c" >blah blah </div>
<div class = "c" >blah blah </div>

I want to wrap all the above tags into a container as follows

<div class="container" >
    <div class = "c" >blah blah </div>
    <div class = "c" >blah blah </div>
    <div class = "c" >blah blah </div>
</div>

I can do it using jQuery, I want learn how this is done in Pure Javascript ( I mean without any library or framework ) . Can someone share the code to warp elements in javascript.

Thank you !!

+10  A: 

You can do like this:

// create the container div
var dv = document.createElement('div');
// get all divs
var divs = document.getElementsByTagName('div');
// get the body element
var body = document.getElementsByTagName('body')[0];

// apply class to container div
dv.setAttribute('class', 'container');

// find out all those divs having class C
for(var i = 0; i < divs.length; i++)
{
   if (divs[i].getAttribute('class') === 'C')
   {
      // put the divs having class C inside container div
      dv.appendChild(divs[i]);
   }
}

// finally append the container div to body
body.appendChild(dv);
Sarfraz
Good answer, but one nitpick/question - `document.body` is supported in every browser and is surely more efficient that searching the DOM for all instances of body and selecting by array index?
lucideer
@lucideer: That's true probably but i just went with convention :)
Sarfraz
@lucideer: “document.body... is surely more efficient” PREMATURE OPTIMISATION, I want to see screenshots of your benchmarks of `document.body` versus `document.getElementsByTagName('body')` in all modern browsers IMMEDIATELY :)
Paul D. Waite
@Paul - Ha. Touché, but I'm not really too into dogmatic assertions like laws on the prematurity of optimisation - if something is obviously more efficient without impairing readability, maintainability, portability, et al... yadayada. `document.body` is at the very least less verbose. And prettier. There's a lot to be said for pretty code.
lucideer
@sAc - It seems to me that this script only puts the 2nd and above div.c into div.container. Also, how does the script manage to move the div.c elements into div.container? I would have thought that appendChild would be creating new copies of the div.c elements.
KatieK
@KetieK: The `appendChild` can be used to append either new elements or existing ones.
Sarfraz
@lucideer: document.body does read nicer.
Paul D. Waite
@KatieKTechnically you should need to run `removeChild` first here, but running `appendChild` on an element that's already in DOM triggers an explicit `removeChild` call first (the spec. says all browsers should do this, and all do afaik).
lucideer
+3  A: 

sAc has answereed excellently. Personally I'd use this (possibly more complex - depends on your preference) slightly alternative method:

* snip *

... which apparently doesn't function correctly in IE. In IE, it will only work on AJAX-ed documents (which isn't what the OP here is looking for obviously), a limitation I was previously unaware of. Apologies.

lucideer
Which.. doesn't work in IE since it doesn't support evaluate or selectNodes?
meder
That's odd as I've used this plenty of times before in IE. You're right though - the above code doesn't work, I'll debug.
lucideer
+2  A: 

If you're target browsers support it, the document.querySelectorAll uses CSS selectors:

var targets = document.querySelectorAll('.c'),
  head = document.querySelectorAll('body')[0],
  cont = document.createElement('div');
cont.class = "container";
for (var x=0, y=targets.length; x<y; x++){
  con.appendChild(targets[x]);
}
head.appendChild(cont);
Rixius
A: 

You also can use the wrap-function of prototype...

see: http://api.prototypejs.org/dom/element/wrap/

blavla
he said "pure javascript." in other words, he prefers not to use a specific javascript library, but rather his own homegrown code.
samandmoore