1

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?

GWorking
  • 4,011
  • 10
  • 49
  • 90
  • `document.querySelectorAll(".prop2").forEach(function() {})` Your question is an X/Y problem... – mplungjan Aug 26 '16 at 05:31
  • You can push necessary elements into array, and then just iterate over it. – GlebkaF Aug 26 '16 at 05:32
  • You know you can use `document.querySelectorAll('.prop2')` to get all of your `prop2` nodes at once, right? You can even be more specific: `document.querySelectorAll('.pre1 > .prop2')` will get only those nodes that are direct children of `pre1`... – Akshat Mahajan Aug 26 '16 at 05:32
  • To be honest, if this bit of code is not bottlenecking the performance of your application I won't be too bothered about optimizing it. Are we talking about 100k of nodes; where it is slowing down the application drastically? – Samuel Toh Aug 26 '16 at 05:34
  • @SamuelToh it was more a theoretical question, but I have already applied the solution to a function in my code and now instead of waiting 1 second it happens instantaneously (and it actually is a significant deal) – GWorking Aug 26 '16 at 05:47

2 Answers2

2

Your problem can be solved by using querySelectorAll

Note we use a for loop to iterate although some browsers will allow forEach.

More ways to iterate: How to loop through selected elements with document.querySelectorAll

window.onload=function() {
  var prop2 = document.querySelectorAll("#main .prop2");
  for (var i=0, n=prop2.length; i<n; i++) {
    prop2[i].innerHTML="hola";
  }
}    
<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>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Try below code

   var ele=document.querySelectorAll(".prop2");
   var l=ele.length;
   for(i=0;i<l;i++){
    ele[i].innerHTML="hello";
   }
Rakesh Sojitra
  • 3,538
  • 2
  • 17
  • 34