11522 sujets

JavaScript, DOM et API Web HTML5

Bonjour,

d'un côté un menu déroulant avec auto-complétion :


<!doctype html>
<html>

<body>
     <form autocomplete="off" >

     <div class="autocomplete">
     <input id="myInput" type="text" name="MonChoix" placeholder="Recherche">
     </div>
     <input type="submit" value="Valider" onclick="loadDoc()">

    </form>
	
    <div id="resultat">ZONE DE RESULTATS</div>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("resultat").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "resultat.html", true);
  xhttp.send();
}

function autocomplete(inp, arr) {
 
  var currentFocus;
  
  inp.addEventListener("input", function(e) {
      var a, b, i, val = this.value;
      closeAllLists();
      if (!val) { return false;}
      currentFocus = -1;
      a = document.createElement("DIV");
      a.setAttribute("id", this.id + "autocomplete-list");
      a.setAttribute("class", "autocomplete-items");
      this.parentNode.appendChild(a);
      for (i = 0; i < arr.length; i++) {
        if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
          b = document.createElement("DIV");
          b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
          b.innerHTML += arr[i].substr(val.length);
          b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
          b.addEventListener("click", function(e) {
              inp.value = this.getElementsByTagName("input")[0].value;
              closeAllLists();
          });
          a.appendChild(b);
        }
      }
  });
  inp.addEventListener("keydown", function(e) {
      var x = document.getElementById(this.id + "autocomplete-list");
      if (x) x = x.getElementsByTagName("div");
      if (e.keyCode == 40) {
        currentFocus++;
        addActive(x);
      } else if (e.keyCode == 38) {
        currentFocus--;
        addActive(x);
      } else if (e.keyCode == 13) {
        e.preventDefault();
        if (currentFocus > -1) {
          if (x) x[currentFocus].click();
        }
      }
  });
  function addActive(x) {
    if (!x) return false;
    removeActive(x);
    if (currentFocus >= x.length) currentFocus = 0;
    if (currentFocus < 0) currentFocus = (x.length - 1);
    x[currentFocus].classList.add("autocomplete-active");
  }
  function removeActive(x) {
    for (var i = 0; i < x.length; i++) {
      x[i].classList.remove("autocomplete-active");
    }
  }
  function closeAllLists(elmnt) {
    var x = document.getElementsByClassName("autocomplete-items");
    for (var i = 0; i < x.length; i++) {
      if (elmnt != x[i] && elmnt != inp) {
        x[i].parentNode.removeChild(x[i]);
      }
    }
  }
  document.addEventListener("click", function (e) {
      closeAllLists(e.target);
  });
}
var choix = ["A_resultat_1","B_resultat_2","C_resultat_3","D_resultat_4"
];

autocomplete(document.getElementById("myInput"), choix);
</script>
</body>
</html>


de l'autre une page html avec les différents résultats possibles

<!doctype html>
<html>


<body>
	<div id="A_1">A_resultat_1</div>
	<div id="B_2" >B_resultat_2</div>
	<div id="C_3" >C_resultat_3</div>
	<div id="D_4" >D_resultat_4</div>
			
</body>
</html>


Comment afficher le bon résultat sur la zone dédiée de ma première page ?
j'ai consulté beaucoup de tuto mais ne m'en sort pas !
Modifié par guilmot74 (09 May 2020 - 11:31)