For a given amount of html nodes, such as
<div id="main">
<div class="pre1">
<div class="prop1"></div>
<div class="prop2"></div>
<div class="prop3"></div>
</div>
<div class="pre2">
<div class="prop1"></div>
<div class="prop2"></div>
<div class="prop3"></div>
</div>
<div class="pre3">
<div class="prop1"></div>
<div class="prop2"></div>
<div class="prop3"></div>
</div>
</div>
The way to add text to the prop2 nodes would be something like
var el = document.getElementById('main').firstElementChild;
while (el) {
var el2 = el.firstElementChild;
while (el2) {
if (el2.className == 'prop2')
el2.textContent = 'hola';
el2 = el2.nextElementSibling;
}
el = el.nextElementSibling;
}
https://jsfiddle.net/g9zoa1vz/
The problem is that conceptually, the first time that you locate the 'prop2' item, if you know that your data structure is constant, you are wasting time in searching again the position.
The question is: Is there a way to store the DOM location so that after your first search, then you can do something like
var el = document.getElementById('main').firstElementChild;
while (el) {
el.firstElementChild.nextElementSibling.textContent = 'hola';
el = el.nextElementSibling;
}
and then saving significant loop-time when many nodes are to be analyzed?