0

I want to highlight the specific word index passed by function, initially function added on a button click. The problem is when i reassign 'document.getElementById('inputText').innerHTML = filterText;' it re-renders the page(Because there is iframe video ) and there is an iframe and another thing which are conflicting.

function makeALlIndex() {
    defaultText = document.getElementById('inputText').innerHTML;
}

function changeColor(index, word) {
    myText = defaultText;
    var filterText = myText.substr(0, index) + '<span  class="highlightClass">' + word + '</span>' + myText.substr(index + word.length);
    document.getElementById('inputText').innerHTML = filterText;
}

Html

<body onload="makeALlIndex()">
       <div id="inputText">
   Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.
<button onclick="changeColor(0,'')">click me</button>
    </div>
</body>
naCheex
  • 1,131
  • 3
  • 14
  • 34

2 Answers2

0

try to change your method to

function changeColor(index, word) {
   myText = defaultText;
   var filterText = myText.substring(0, index).split( word ).join( '<span  class="highlightClass">' + word + '</span>' ) +  myText.substr(index + word.length);
   document.getElementById('inputText').innerHTML = filterText;
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

@Aliraza -

I think following solution will work for you

function changeColor(index, word) {
    myText = defaultText;
    var filterText = '',newText ='',myTextArray =[];
    if( myText.indexOf(word)!=-1){
        myTextArray = myText.split(word);
        for(i=0; i<= myTextArray.length-1;i++){  
           if(i==index){
               newText = myTextArray[i] + '<span  class="highlightClass">' + word + '</span>';
            }else {
               newText =  myTextArray[i];
            }
            filterText += newText;
       }    
        document.getElementById('inputText').innerHTML = filterText;
    }
}    

It is based on the URL you have provided. Still there are lots of things you may need to change in it.

paraS elixiR
  • 1,866
  • 1
  • 18
  • 26