mercredi 3 juillet 2019

How to fix "Uncaught TypeError: Cannot set property 'innerHTML' of null"

My code compares two arrays and shows items that are in both arrays. I can get this to successfully print to the console to transform a paragraph is proving more challenging.

Uncaught TypeError: Cannot set property 'innerHTML' of null at compareArrays (pen.js:25) at HTMLButtonElement.onclick (index.html?key=iFrameKey-ae252606-6389-a594-b844-2cecca064c7d:20) compareArrays @ pen.js:25 onclick @ index.html?key=iFrameKey-ae252606-6389-a594-b844-2cecca064c7d:20

I understand I have to use the innerHTML method but unsure what my errors mean

<button id="search" onclick="compareArrays()" required>Search</button>
<p id="results"></p>

function compareArrays () {
  // Interate the first array
  for (var i = 0; i < restaurantsOne.length; i++) {
    // Iterate the second array 
    for (var j = 0; j < restaurantsTwo.length; j++){
      // Compare the two arrays
      if (restaurantsOne[i] == restaurantsTwo[j]){
        // console.log(restaurantsOne[i]);
        // Show results in paragraph
        document.getElementById("#results").innerHTML = restaurantsOne[i] 

      }
    }
  }
 }

Ultimate I would like each item, restaurantsOne[i], that is in both arrays to print to a new paragraph.

Aucun commentaire:

Enregistrer un commentaire