title: How do I activate a function with a second <button> to call a forEach?
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let words = [];
  var longestCt = 0;
  var longestWord = "";
  var myList = [];
  var myList = ['Loops','are','used','while','Learning JavaScript'];
  
  function addWords() {
      let template = words.map(word =&gt; `&lt;li&gt;${word}&lt;/li&gt;`).join('\n');
      document.querySelector('ul').innerHTML = template;
  }
  
  addWords();
  
  let btnAdd = document.querySelector('button');
  let input = document.querySelector('input');
  
  
  btnAdd.addEventListener('click', () =&gt; {
      words.push(input.value);
      input.value = '';
      addWords();
  });
  
  let findLong = document.querySelector('button');
  
  findLong.addEventListener('click', () =&gt; {
     
      let longestCt = 0;
      let longestWord = "";
  
      words.forEach(function (value) {
          if (value.length &gt; longestCt) {
              longestWord = value;
              longestCt = value.length;
          } else {
              longestWord = longestWord;
          }
      });
  
      //}
  });
  document.getElementById("demo2").innerHTML = longestWord;</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>div {
      widows: 20%;
      margin: auto;
  }
  
  input, button {
      padding: 5px;
      display: inline-block;
  }
  
  li {
      font-size: 20px;
      font-weight: bold;
      margin-left: -40px;
      list-style: none;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  
  &lt;head&gt;
  
      &lt;link rel="stylesheet" href="test_Arr_Input.css"&gt;
      &lt;title&gt;Test Longest Array.html&lt;/title&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;!--Thank you to YouTube KodeBase with my modifications adding a second button which isn't working--&gt;
  
  &lt;div&gt;
  
      &lt;input type="text"&gt;
  
      &lt;button&gt;Add a Word&lt;/button&gt;
  
      &lt;ul&gt;&lt;/ul&gt;
  
      &lt;br&gt;
  
      &lt;button&gt;Find the Longest&lt;/button&gt;
  
      &lt;p id="demo"&gt;&lt;/p&gt;
  
  &lt;/div&gt;
  
  &lt;script type="text/javascript" src="test_Arr_Input.js"&gt;&lt;/script&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Full disclosure; Beginner. With that said; thank you in advance.</p>
  <p>I have written a simple JavaScript to return the longest string in an array.
  <strong>Here it is as simply a JavaScript function, that I ran from the console,</strong> without interacting with the html:</p>
  <pre><code>var myList = ['Loops','are','used','while','Learning JavaScript'];
  
  var longestCt = 0;
  var longestWord = &quot;&quot;;
  
  myList.forEach(function (value) {
      if (value.length &gt; longestCt) {
          longestWord = value; longestCt = value.length;
      } else {
          longestWord = longestWord;
      }
  });
  console.log(longestWord);
  </code></pre>
  <p>I then endeavored to create an HTML page to enter objects into the array and to create a list.
  <em>(Thanks to KodeBase from Youtube for the direction).</em> The JavaScript for this is posted at the end after the CSS. <strong>Here is the HTML:</strong></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;head&gt;
  
      &lt;link rel=&quot;stylesheet&quot; href=&quot;test_Arr_Input.css&quot;&gt;
      &lt;title&gt;Test Longest Array.html&lt;/title&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;!--Thank you to YouTube KodeBase with my modifications adding a second button which isn't working--&gt;
  
  &lt;div&gt;
  
      &lt;input type=&quot;text&quot;&gt;
  
      &lt;button&gt;Add a Word&lt;/button&gt;
  
      &lt;ul&gt;&lt;/ul&gt;
  
      &lt;br&gt;
  
      &lt;button&gt;Find the Longest&lt;/button&gt;
  
      &lt;p id=&quot;demo&quot;&gt;&lt;/p&gt;
  
  &lt;/div&gt;
    
      &lt;button onclick=&quot;intoMyList()&quot;&gt;Submit to the array&lt;/button&gt;
  
      &lt;p id=&quot;demo&quot;&gt;&lt;/p&gt;
  
      &lt;button onclick=&quot;findTheLongest()&quot;&gt;Find the Longest Word&lt;/button&gt;
  
      &lt;p id=&quot;demo2&quot;&gt;&lt;/p&gt;
  
  &lt;script type=&quot;text/javascript&quot; src=&quot;test_Arr_Input.js&quot;&gt;&lt;/script&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <p><strong>Here is the CSS:</strong></p>
  <pre><code>div {
      widows: 20%;
      margin: auto;
  }
  
  input, button {
      padding: 5px;
      display: inline-block;
  }
  
  li {
      font-size: 20px;
      font-weight: bold;
      margin-left: -40px;
      list-style: none;
  }
  </code></pre>
  <p><strong>And here is the final JavaScript:</strong></p>
  <pre><code>let words = [];
  
  function addWords() {
      let template = words.map(word =&gt; `&lt;li&gt;${word}&lt;/li&gt;`).join('\n');
      document.querySelector('ul').innerHTML = template;
  }
  
  addWords();
  
  let btnAdd = document.querySelector('button');
  let input = document.querySelector('input');
  
  
  btnAdd.addEventListener('click', () =&gt; {
      words.push(input.value);
      input.value = '';
      addWords();
  });
  
  let findLong = document.querySelector('button');
  
  findLong.addEventListener('click', () =&gt; {
      //function findTheLongest() {
      let longestCt = 0;
      let longestWord = &quot;&quot;;
  
      myList.forEach(function (value) {
          if (value.length &gt; longestCt) {
              longestWord = value;
              longestCt = value.length;
          } else {
              longestWord = longestWord;
          }
      });
  
      //}
  });
  document.getElementById(&quot;demo2&quot;).innerHTML = longestWord;
  </code></pre>
  <p>First I changed a few variable names for more clarity.</p>
  <p>But the issue is, although I am able to get the words to enter the array by clicking the first button, the second button does not seem to call the ForEach function to trigger the return of the longest word. I am wondering if I need to somehow differentiate between button 1 and 2 in the HTML. Or if I just need to go back to the drawing board to rethink my understanding of the way I am putting the JS together.</p>