0

This is my html code:

<!DOCTYPE html>
<html>
<head>
<script>
function Check(e) {
  var text=document.getElementById("text");
  var inputnum = document.getElementById("number");
  if (e.value == "option1") {
    inputnum.min = "5";
    inputnum.max = "10";
    text.innerHTML ="input the value from 5 to 10";
  } else if (e.value == "option2") {
    inputnum.min = "20";
    inputnum.max = "30";
    text.innerHTML ="input the value from 20 to 30";
  }
}
</script>
</head>
<body>


<select id="selectMe" onChange="Check(this);">
   <option value=""></option>
    <option value="option1">option1</option>
    <option value="option2">option2</option>
</select>
<br><br><br>

<input name="number" type= "number" id="number">
<p id="text"></p>
</input>

</body>
</html> 

I need little modification in this code which sets the minimum and maximum value of the number input box as per the option selected from drop down.

For eg: If user selects option1 then the minimum value must be 5 and maximum most be 10. Similarly If user selects option2 then the minimum value must be 10 and maximum most be 20. And it also should show the message (inside html below the text box (not as alert message) showing please input the value from xxx to xxx

Please help me with this. thankyou in advance.

1 Answers1

0

See my example, when selectbox change (so use onChange), active a function check the value of selectbox then change min/max of input

  var text=document.getElementById("text");
  var inputnum = document.getElementById("number");
window.onload = function() { inputnum.disabled=true; text.innerHTML =""; };
function Check(e) {

  if (e.value == "option1") {
    inputnum.disabled=false;
    inputnum.min = "5";
    inputnum.max = "10";
    text.innerHTML ="input the value from 5 to 10";
  } else if (e.value == "option2") {
    inputnum.disabled=false;
    inputnum.min = "20";
    inputnum.max = "30";
    text.innerHTML ="input the value from 20 to 30";
  } else if (e.value == "empty"){
  inputnum.disabled=true;
  text.innerHTML ="";
  
  }
}
<select id="selectMe" onChange="Check(this);">
  <option value="empty"></option>
  <option value="option1">option1</option>
  <option value="option2">option2</option>
</select>
<br /><br /><br />

<input name="number" type="number" id="number" />
<p id="text"></p>

the function must be in <script></script> tag.


Add after comment:

i add onload function, where page is load input is disabled.

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34