Tutorial :Change Element's node name?



Question:

Is it possible to change the element's node name in GWT? I mean something like this:

HTML h = new HTML();  h.getElement().setNodeName("mydiv")  

while there is no setNodeName() method for Element.

I'd like to acquire <mydiv>some contents</mydiv> instead of default tag <div>some contents</div>

Thanks for any hints.


Solution:1

You can't change the element node name of the HTML widget. However, you can create your own tag with Document.get().createElement("mydiv"), and use that to create a new Widget by extending Composite. However, I'm not sure why you want to do this, because adding new tags to the DOM and thereby extending HTML doesn't sound as something you should want. Setting the content in this tag isn't possible via methods like innerText because they are only available for valid tags.


Solution:2

change the tag name while keeping content and attributes

function changeTagName(elm,new_tag_name){    var newElm = document.createElement(new_tag_name)    var atr = elm.attributes;           for(var i=0;i<atr.length;i++){  // copy all atributtes       newElm.setAttribute(atr[i].name,atr[i].value)    }     document.body.insertBefore(newElm,elm)    newElm.innerHTML=elm.innerHTML; //copy the content    elm.parentNode.removeChild(elm)  // remove original   }  

for example:

<span id='sp1' class='cl1 cl2'> some t  e x t with   (\n)         gaps .... and etc</span>    changeTagName(document.getElementById('sp1'),'pre');  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »