rafalry
rafalry

Reputation: 2670

Change Element's node name?

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.

Upvotes: 0

Views: 646

Answers (2)

bortunac
bortunac

Reputation: 4808

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');

Upvotes: 1

Hilbrand Bouwkamp
Hilbrand Bouwkamp

Reputation: 13519

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.

Upvotes: 2

Related Questions