One script for repeated divs

Greetings.

I need to remove everything after the first coma in a dynamically generated content into this div:

<div class="nest">
  <div class="cont">
    <div class="format">
       element, element2, element3
    </div>
  </div>
</div>

To get this:

<div class="nest">
  <div class="cont">
    <div class="format">
       element
    </div>
  </div>
</div>

I have solved it by this script:

<script>
var format = document.querySelector(".format").textContent;
var format2 = format.split(",", 1);
document.querySelector(".format").textContent = format2;
</script>';

But now I realize that there are several pages (search results) that repeat the structure above, and the script works just for the first instance.

I have tried to do it with querySelectorAll following this example:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_document_queryselectorall_loop_target

Without much success to tell the truth.

I would appreciate some help or hint.

Thanks.

I suggest you use document.querySelectorAll() and iterate within a loop.
The documentation clearly explains the difference between document.querySelector and document.querySelectorAll

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.