title: Verify if an element is present in an array
body: <p>I'm trying to develop an algorithm that allows the user to enter a word in a text field, if this word already exists in the datalist then sends me an alert that the word entered already exists. </p>
  
  <p>In fact, my code allows me to do this except that if I try to choose a value from the datalist it will send me the alert.</p>
  
  <p>Here is my code:</p>
  
  <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>$("input[type='text'][name^='Q0']").change(function() {
    verif(this.value, this.id);
  });
  
  function verif(val, id) {
    if (val != "") {
      val = val.toLowerCase();
  
      //1.getting datalist values as an array :
      var array = $('datalist option').map(function() {
        return this.value;
      }).get();
  
      //2.Checking if string exist in datalist:      
      for (var i = 0; i &lt; array.length; i++) {
        if (val.indexOf(array[i]) != -1) {
          console.log("le mot existe dans la liste");  // alert("...") replaced with console.log()
          document.getElementById(id).value = "";
        }
      }
    }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;input type="text" name="Q0" id="Q0_98" autocomplete="off" list="Q0_98_List" size="100%" /&gt;
  &lt;datalist id="Q0_98_List"&gt;
    &lt;option value="valeur 1"&gt;&lt;/option&gt;
    &lt;option value="valeur 2"&gt;&lt;/option&gt;
    &lt;option value="valeur 3"&gt;&lt;/option&gt;
  &lt;/datalist&gt;
  &lt;/br&gt;
  &lt;input type="button" value="PRECEDENT" /&gt;
  &lt;input type="button" value="SUIVANT" /&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Can you help me please?</p>