knagurski
02-27-2007, 07:50 AM
With the ideas of progressive enhancement, often I find it necessary to wrap the contents of a tag with another tag. In other words, I wanted to do the following transformation:
<h2>Heading</h2>
<h2><span>Heading</span></h2>
Jack provides a great way of wrapping a tag but it really didn't fit my needs, so I made my own little addition to Ext.
Add the following to your JS and you should be good to go.
Ext.Element.prototype.wrapContents = function ( dhConfig, returnDom ) {
dhConfig.html = this.dom.innerHTML;
return Ext.DomHelper.overwrite( this.dom, dhConfig, !returnDom );
};
Ext.CompositeElement.createCall(Ext.CompositeElement.prototype, "wrapContents");
To wrap the contents of an element you can use one of the following
//using a flyweight element
Ext.fly( 'myDiv' ).wrapContents( { tag: 'span' } );
//using a standard element
Ext.get( 'myDiv' ).wrapContents( { tag: 'span' } );
//using a CompositeElement to wrap contents of all h2 tags with a span
Ext.select( 'h2' ).wrapContents( { tag: 'span' } );
There are two arguments that can be passed, the first is a DomHelper object (ommiting the html attribute), the second is whether to return a DOM reference to the new tag, or an Ext Element.
I hope someone else finds this useful. :)
BTW, thanks to Animal for helping me get the function to work with CompositeElements :)
<h2>Heading</h2>
<h2><span>Heading</span></h2>
Jack provides a great way of wrapping a tag but it really didn't fit my needs, so I made my own little addition to Ext.
Add the following to your JS and you should be good to go.
Ext.Element.prototype.wrapContents = function ( dhConfig, returnDom ) {
dhConfig.html = this.dom.innerHTML;
return Ext.DomHelper.overwrite( this.dom, dhConfig, !returnDom );
};
Ext.CompositeElement.createCall(Ext.CompositeElement.prototype, "wrapContents");
To wrap the contents of an element you can use one of the following
//using a flyweight element
Ext.fly( 'myDiv' ).wrapContents( { tag: 'span' } );
//using a standard element
Ext.get( 'myDiv' ).wrapContents( { tag: 'span' } );
//using a CompositeElement to wrap contents of all h2 tags with a span
Ext.select( 'h2' ).wrapContents( { tag: 'span' } );
There are two arguments that can be passed, the first is a DomHelper object (ommiting the html attribute), the second is whether to return a DOM reference to the new tag, or an Ext Element.
I hope someone else finds this useful. :)
BTW, thanks to Animal for helping me get the function to work with CompositeElements :)