@nwhite
The problem with this implementation is that text-nodes inside the
element are not 'wrapped'.
The getChildren() call will only copy the real elements.
Here is the routine I'm using, which is based on standard DOM calls,
but perfectly mixes with mootools Elements.
It will copy all child nodes of the element.
wrapContent : function(el){
while( el.firstChild ) this.appendChild( el.firstChild );
el.appendChild( this ) ;
return this;
}
dirk
On Aug 28, 1:37 am, nwhite <[email protected]> wrote:
> Your solution is far from ideal. What your doing to the DOM is brutal. Your
> recreating every node in the page. By doing this any event listeners you
> have on an element is lost.
>
> The solution I provided above is superior because its faster, and doesn't
> break existing events.
>
> If the syntax seemed too much (a one liner) you could implement your own
> method on element.
>
> Element.implement({
> wrapContent : function(el){
> this.adopt($(el).getChildren()).inject(el);
> }
> });
>
> usage:
>
> new Element('div',{ 'id' : 'wrapper').wrapContent(document.body);
>
>
>
> On Thu, Aug 27, 2009 at 1:18 PM, atwork8 <[email protected]> wrote:
>
> > @Steve - I have no control over the html as it comes from a large CMS,
> > obviously that would have been the initial solution.
>
> > I appreciate there are many ways of achieving the same result, the
> > solution I provided seemed to be the shortest, and I wondered if there
> > was a more 'moo' way of doing it. From what you are saying it would
> > seem that this isn't the case.
>
> > Cheers
>
> > On Aug 27, 4:20 pm, "Steve Onnis" <[email protected]> wrote:
> > > To answer your question, there are many ways to do it.
>
> > > I would probably do it like this..
>
> > > new Element("div", {"id":"wrapper"}).set("html",
> > > $(document.body).get("html")).inject($(document.body).empty())
>
> > > You ask if what you showed is the best way? For me...if it works then
> > great
> > > :)
>
> > > -----Original Message-----
> > > From: atwork8 [mailto:[email protected]]
> > > Sent: Thursday, 27 August 2009 8:53 PM
> > > To: MooTools Users
> > > Subject: [Moo] wrap element's html
>
> > > Hi,
>
> > > I've checked the docs but I can't see any method for wrapping the html
> > > of an element, not the whole element. If I wanted to add a wrapper
> > > around the body's html is this the best way of doing it:
>
> > > var elBody = $(document.body);
> > > elBody.set('html', '<div id="wrapper">' + elBody.get('html') + '</
> > > div>');