0

Code:

function checkInput() {
  var input1 = document.getElementById("on").value;
  var input = document.getElementById("off").value;

  if (input1 === "on") {
    document.getElementById('reponse').innerHTML = "LED EST ON";
    return false;
  }
  
  if (input === "off") {
    document.getElementById('reponse').innerHTML = "LED EST off";
    return false;
  }
}
<form action="url" method="POST">
  Allumer la LED D7 <br>
  <br>
  <input type="radio" name="arg" value="on" id="on">On.
  <br>
  <input type="radio" name="arg" value="off" id="off">Off.
  <br>
  <br>
  <input type="submit" value="OK" onclick="checkInput()">
  <br>
</form>
<p id="reponse"> </p>

i want to if i selected On and submitted i get a message "LED EST ON";

and if i selected Off and submitted i get "LED EST Off"

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
elhex
  • 23
  • 2

1 Answers1

1

You can do that in a more simpler way like the following:

function checkInput(){
  var input = document.querySelectorAll("input[type=radio]:checked");
  if (input[0].getAttribute('value') == 'on') {
  document.getElementById('reponse').innerHTML  = "LED EST ON";
    return false;
  }  
  else{
    document.getElementById('reponse').innerHTML  = "LED EST off";
    return false;
  }
}
<input type ="radio" name="arg" value="on" id="on" >On.
<br>
<input type="radio" name="arg" value="off" id="off" >Off.
<br>
<br>
<input type="submit" value="OK" onclick="checkInput()" >
<br>
<p id="reponse" > </p>
Mamun
  • 66,969
  • 9
  • 47
  • 59