A
A
Art. Ku.2018-01-21 07:54:33
JavaScript
Art. Ku., 2018-01-21 07:54:33

How to traverse all nodes inside a parent (children, grandchildren, great-grandchildren, ...)?

Hello. Tried recursion. But the first return ends the function, and without it - an eternal loop.
Here: jsfiddle.net/kukaew/n6qpw2ga
Thanks.

<mark class="obj"><u><a>т</a><strike><b>уп</b><a>и</a></strike><a>к</a></u></mark>

<div id=w></div>

var obj = document.getElementsByClassName('obj')[0];

Recurs(obj);

function Recurs(obj) {
  if ( obj.nodeType === 3) w.innerText = w.innerText + obj.textContent;	
  else for (i = 0; i < obj.childNodes.length; i++) return Recurs(obj.childNodes[i]);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-01-21
@kukaew

https://jsfiddle.net/p22264uz/
and the text is copied in one line at all, without any recursions https://jsfiddle.net/xr9wh0v4/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question