0

For a div that appears multiple times in your HTML :

<div class="myclass">abc</div>
<div class="myclass">abc def</div>
...

How can I achieve the replacement of "abc" for all the occurrences ? (I found resources to replace the first occurrence but not the rest)

This is my code with javascript method

<script>
 function addLoadEvent(func) {
   var oldonload = window.onload;
   if (typeof window.onload != 'function') {
     window.onload = func;
   } else {
     window.onload = function() {
       if (oldonload) {
         oldonload();
       }
       func();
     }
   }
 } 

 addLoadEvent(function() {  

    oldText = document.getElementsByClassName("myclass").innerHTML; 
    for (i = 0; i<oldText.length; i++){ 
    newText = oldText[i].innerHTML;
    newText = newText.replace(/abc/g, "123");
    oldText[i].innerHTML = newText;

    });
</script>
3
  • 2
    There is literally no jquery in that example? It's vanilla javascript but your also loading jquery for no reason! What exactly is the question here, does that not work? Commented Jan 11, 2017 at 14:59
  • yes it is not working, i am trying to find a good method to replace the inner text in the div that occurs multiple times. Maybe I confused myself between jquery and JS... Commented Jan 11, 2017 at 15:02
  • 1
    Your code works, just remove .innerHTML from document.getElementsByClassName("myclass") Commented Jan 11, 2017 at 15:04

2 Answers 2

3

Here is solution:

The problem is at this line: oldText = document.getElementsByClassName("myclass").innerHTML;

.innerHTML method returns HTML content.

You only need to get elements by class name, and obtain a nodeList.

oldText = document.getElementsByClassName("myclass");

function addLoadEvent(func) {
   var oldonload = window.onload;
   if (typeof window.onload != 'function') {
     window.onload = func;
   } else {
     window.onload = function() {
       if (oldonload) {
         oldonload();
       }
       func();
     }
   }
 }
 addLoadEvent(function() {  
    oldText = document.getElementsByClassName("myclass"); 
    for (i = 0; i<oldText.length; i++){ 
      newText = oldText[i].innerHTML;
      newText = newText.replace(/abc/g, "123");
      oldText[i].innerHTML = newText;
    }
 });
<div class="myclass">abc</div>
<div class="myclass">abc def</div>

Sign up to request clarification or add additional context in comments.

Comments

0

target every class, split value, remove 0 index and concatenate.

  $('.myclass').each(function(){
       var data = $(this).val();
       var data_array = data.split();
       var new = '';
       for(var i=1; i<data_array.length; i++){
          var _new += ' '+data_array[i];
       }
       $(this).val(_new);
    });

2 Comments

I don't think the OP wants jquery. He doesn't know what it is
new is a keyword and cannot be used as a variable name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.