0

I load some highlights as a json and i look for the selection in HTML.

function loadCorrections1(){
  
  $.ajaxSetup({
      async: false
  });

  $.getJSON('../loadCorrections.php?number=<?php echo $number?>&idDoc='+$('#idDoc').val(), 
   function(data) {
    highlights = [];
    $.each(data, function(index) {
        //test si la correction pour chaque highlight est null
        if(data[index].highlight != null && data[index].correction == null){
          var text = data[index].highlight;
          highlights.push(text);
          idTables.push(data[index].id);
        }
    });
  });
  
}

and i highlight them like this way:

function highlightErrors(){
  loadCorrections1();
  var txt = document.getElementsByTagName("body")[0];
  var innerHTML = txt.innerHTML;
  for(i=0;i<highlights.length;i++){

    var text = highlights[i];
    var inner = innerHTML;
    //supression d espce a la fin de la phrase
    if(text.endsWith(' ')){
      text = text.substring(0,text.length - 1);
    }
    //supression d espce en debut de phrase
    if(text.startsWith(' ')){
      text = text.substring(1,text.length);
    }
    var index = inner.indexOf(text);
    console.log(text);
    console.log(index);

    if (index >= 0) { 
      innerHTML = inner.replace(text,"<span class='thisSpan' onclick='Unhighlight(); return false' onmouseover='correctSentence(event);hover(this);' onmouseout='unhover(this);' style='background-color: yellow;'> "+text+" </span>");
      txt.innerHTML = innerHTML;
    }
  }
  $("#correct").attr('checked', true);
}

the probleme is some selection i made sure that they look exactly like the innerHTML they actually return -1 i the following line of code : var index = inner.indexOf(text);

i dont understand why this happens.

  • 1
    Replacing the entire `innerHTML` of `body` is not a good idea. All dynamic changes will be lost, such as event listeners, form inputs. – Barmar Aug 11 '21 at 23:40
  • what are the "some selection" exactly? – vanowm Aug 11 '21 at 23:41
  • 1
    `loadCorrections1()` is asynchronous, but you're not waiting for the AJAX request to finish. – Barmar Aug 11 '21 at 23:42
  • @Barmar yes but all the data i fetch from json is there but the value doesnt match anything in the innerHMTL – Ayoub Mafkoud Aug 11 '21 at 23:45
  • The `for` loop is running before you update the `highlights` array. – Barmar Aug 11 '21 at 23:50
  • @vanowm the selection is a sentence and sometimes there is html tags in it, and m actually having this bug with a selection that has i tried removing the tags from both innerHTML and the selection and i made sure that the selection and the sentence in the html look alike but the problem only occured for one sentence exactly while other sentences it worked just fine. – Ayoub Mafkoud Aug 11 '21 at 23:50
  • @bamar the data in json is all there ,console.log() display it all – Ayoub Mafkoud Aug 11 '21 at 23:52
  • @AyoubMafkoud it's all there, but because it's being loaded through async function, by time it's received your loop already finished. – vanowm Aug 11 '21 at 23:53
  • Do you understand what "asynchronous" means? The data is in the JSON, but you're not waiting for it. – Barmar Aug 11 '21 at 23:53
  • @Barmar i kinda understand what you re sayin but what can be the solution? – Ayoub Mafkoud Aug 11 '21 at 23:57
  • You should do the highlighting in the callback function of `$.getJSON`. – Barmar Aug 11 '21 at 23:58
  • @Barmar i understood what you said and tried the solution but the same problem occurs – Ayoub Mafkoud Aug 12 '21 at 01:11

0 Answers0