I am parsing a website and I get an element like this
<td>
<span class="label">Hometown/High School:</span>
"
Austin, TX
/
Westwood
"</td>
Problem is when I'm manipulating the text nodes, i get a node like--> "
Austin, TX
/
Westwood
"
Its parent comes null. And I want to split this text on '/' and replace it with tags like <sometag>Austin,Tx</sometag> <sometag>Westwood</sometag>
But not able to do it coz the text node's parent is coming null, not able to calculate its xpath.
EDIT : code that I'm using to split and replace the textnode
let parent = textnodeStr.parentElement; // textnodeStr == the text node element
if(parent != null){
parent.innerHTML = '';
let elements = [];
for (var j=0; j< arr.length; j++){ //arr is the array which contains ['Austin, Tx', 'Westwood'] i.e. the substrings I get After I split the above textnode using '/'
elements[j] = document.createElement("rtechContainer");
newText = document.createTextNode(arr[j]);
elements[j].appendChild(newText);
parent.appendChild(elements[j])
}
}
ADDITIONAL INFO I am usinf createTreeWalker to access the text nodes. Here's a log of what I am doing.
- Using createTreeWalker accessing the text nodes.
- Based on some condition, storing selective textnodes in an array (say array selectedTextNodes).
- When treeWalker finishes its execution, call another function through which I access the previously mentioned array (selectedTextNodes).
- Now inside the function, I iterate over the array and try to access the parentNode of each item. Here is what happens.
For text node text1
in
<td><span> "text1" </span></td>
I get the parent node in my function.
For text node text2
in
<td>" text2 "</td>
I get parent null
in my function.
However, I get the required correct parents when I access parentNode of these two text nodes in createTreeWalker itself.