Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HTML only replaces 1st match

How can I make that this code replaces both paragraphs? I also need it to work with lists.

<p class="demo">Mr Blue has a blue house and a blue car blue blu,e blue bl,ue.</p>
<p class="demo">Mr Blue has a blue house and a blue car blu,e blue bl,ue blue.</p>

<script>
let str = document.getElementByClass("demo").innerHTML; 
let res = str.replace(/,/g, ", ");
document.getElementByClass("demo").innerHTML = res;
</script>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Never refer by ID, always use classes.

var els = document.getElementsByClassName('kala');
for (var i = 0; i < els.length; i++) {
  els[i].innerHTML = els[i].innerHTML.replace(/,/g, ', ');
}

This kind of a snippet does the job!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading