0

here i got stucked this error when i check the correctValue is equal to the document.getElementById('box'+j).innerHTML the interpreter see the second one as undefined but when i changed document.getElementById('box'+j).innerHTML with this.innerHTML it work.just i didnt understand why it doesnt work with document.getElementById('box'+j).innerHTML.for the this.innerHTML it refer to the current element this i know it.here is my code below Thank you in advance

//if i click on the answer box
    for(j=1;j<5;j++){
      //here i check every box if checked
      document.getElementById('box'+j).onclick=function(){
        //check if im playing
        if(playing==true){
          //if the inner Html of the current box is equal to the correct value
            if(document.getElementById('box'+j).innerHTML==correctValue){
              //we add 1 to the score
            score+=1;
              //we edit the scorevalue span with the new value of score
            editElement('scoreValue',score);
              //we hide the wrong div
            hide('wrong');
              //show the correect div
            show('correct');
              //set time out of 1 second and after that we hide the correct div 
            setTimeout(function(){ hide('correct'); }, 1000);
              //generate new question and answer
            generateQA();
          }else{//if the values are not equal 
            // hide the correct div 
            hide('correct');
            //show the wrong div
            show('wrong');
            //set a time out of 1 second and after that i show the wrong div
            setTimeout(function(){ hide('wrong'); }, 1000);
          }
        }
      };
    }
user3548898
  • 181
  • 2
  • 2
  • 9
  • Sorry what's the problem? Are you wondering why `this` works? – evolutionxbox Feb 16 '17 at 10:47
  • I think it's more the other way around, he's trying to understand why it doesn't work with getElementById. – Jonas Grumann Feb 16 '17 at 10:47
  • no im wondering why if(document.getElementById('box'+j).innerHTML==correctValue) doesnt work because document.getElementById('box'+j).innerHTML is undefined but when i change it with this.innerHTML it works. is the j variable is undefine inside the onclick function – user3548898 Feb 16 '17 at 10:48

0 Answers0