0

I have

<div>
    blah
    <div>blah2</div>
</div>

I want to erase 'blah' without erasing blah2

How would I do it?

Using the latest dojo, 1.10.

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90

2 Answers2

1

I am not sure whether it can be done only using dojo.
The below code uses a mix of dojo and plain javascript objects to achieve your desired result.

your problem statement.(note: I have added an id attribute "mydiv" to the parent div).

<div id="mydiv">
    blah
    <div>blah2</div>
</div>

Deleting all the text node 'blah'.

// require the query and domReady modules
require(["dojo","dojo/query", "dojo/domReady!" ], function(dojo,query) {
   // retrieve an array of nodes with the ID "list"
   var list = query("#mydiv")[0];
   console.log("list:",list);
   var childNodes = list.childNodes;
   var len = childNodes.length;
   var i;
   for ( i = 0; i < len; i++){
       // Destroy All textnodes.
       if ( childNodes[i].nodeType === 3 ) {
          //console.log ("Text node found");
          dojo.destroy(childNodes[i]);
       };
   } 

})
frank
  • 3,180
  • 3
  • 16
  • 18
  • can you edit the solution to delete all nodes that are text nodes regardless of their position relative to the non-text nodes? – Isaac Bolinger Jan 02 '15 at 08:08
  • I have updated my answer. Also request you to change the question heading something like "Remove text nodes within a give node" – frank Jan 02 '15 at 09:06
0

Courtesy of Tim Down: How to remove text (without removing inner elements) from a parent element using jquery This is supposed to work since IE5 according to him.

require(["dojo/query", "dojo/domReady!"], function(query) {
var parent = query("#parentid")[0];    
var nextchild;
var child = parent.firstChild;

while (child) {
nextChild = child.nextSibling;
if (child.nodeType == 3) {
parent.removeChild(child);
}
child = nextChild;
}
});
Community
  • 1
  • 1
Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90