11480 sujets

JavaScript, DOM et API Web HTML5

bonjour a tous
je cherche une solution pour verifier si le champ ville de mon formulaire est vide ou pas avant l'envois
si vide alors affiche une image
si pas vide alors on affiche pas une image

j'ai essaye ceci mais dans tous les cas une image s'affiche lorsque je passe ma souris dans le rectangle
que le champ soit remplit ou pas de ville

<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmousemove Event</h2>
<input class="form-control" type="text" name="ville" value="" id="ville" placeholder="" required="required" />
<div style ="width: 200px;
height: 100px;
border: 1px solid black;"onmousemove="myFunction(event)" onmouseout="clearCoor()"></div>
<img id="myImgaa" src="" width="107" height="98">
<script>
function myFunction() {
if (document.getElementsByName(ville) !== null && document.getElementsByName(ville) !== '')
{ document.getElementById("myImgaa").src = "https://www.w3schools.com/jsref/img_pulpit.jpg";}
}
function clearCoor() {
document.getElementById("myImgaa").src = "";
}
</script>
</body>
</html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The onmousemove Event</h2>
<input class="form-control" type="text" name="ville" value="" id="ville" placeholder="" required="required" />
<div style ="width: 200px;
height: 100px;
border: 1px solid black;"onmousemove="myFunction(event)" onmouseout="clearCoor()"></div>
<img id="myImgaa" src="" width="107" height="98">
<script>
function myFunction() {
  const ville = document.querySelector('[name="ville"]');
  if (ville !== null && ville.value !== '')
  { 
    document.getElementById("myImgaa").src = "https://www.w3schools.com/jsref/img_pulpit.jpg";
  } else { 
    document.getElementById("myImgaa").src = "";
  }
}
</script>
</body>
</html>

Modifié par flexi2202 (29 Jan 2023 - 23:35)