1
<html>
   <head>
       <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
   </head>
   <body>
      <script>
         function checkValue() {         
         var id=$('#testValue').val();
         if(id < 10){
                $('#bb').text("value is LESS than 10. Disable Submit button please!");
                $('#submitBtn').disabled = true;
            }else{
                $('#bb').text("value is MORE than 10. Enable submit button please!");
                $('#submitBtn').disabled = false;
            }
         }
        function testSubmit(){
            alert('test submit clicked.');
        }
      </script>
      input value:
      <input type="text"  id="testValue" maxlength="6" size="10" required>
      <button type="button" name="action" onclick="checkValue()"    value="issueInvoice">TEST VALUE</button>
      <p style="text-align:left" class="txt-centered label-registersuccess font-red" id="bb"></p>
      <button type="button" name="action" id="submitBtn"  onclick="testSubmit()"    value="issueInvoice">submit button</button> 
   </body>
</html>

I have this simple form. I input a value. If less than TEN, a text is shown, 'value less than 10'. I also want to disable the button on this condition. how do i do it? Then when value is more than TEN, a ''value more than 10 is shown' and button enabled.

how do i do this? $('#submitBtn').disabled = true; does not work :(

Jacel
  • 307
  • 4
  • 10
  • 24

4 Answers4

10
$('#submitBtn').prop('disabled', true);
vikalp
  • 330
  • 2
  • 9
  • 2
    It would be helpful to add an explanation to your answer so the OP will learn why this works. – Sam Mar 13 '18 at 13:14
1

You can use attr and removeAttr properties to solve it...

         function checkValue() {         
         var id=Number($('#testValue').val());
         if(id < 10){
                $('#bb').text("value is LESS than 10. Disable Submit button please!");
                $('#submitBtn').removeAttr('disabled');
            }else{
                $('#bb').text("value is MORE than 10. Enable submit button please!");
                $('#submitBtn').attr('disabled','true');
            }
         }
        function testSubmit(){
            alert('test submit clicked.');
        }
       <script src="https://code.jquery.com/jquery-1.10.2.js"></script>

 
      input value:
      <input type="text"  id="testValue" maxlength="6" size="10" required>
      <button type="button" name="action" onclick="checkValue()"    value="issueInvoice">TEST VALUE</button>
      <p style="text-align:left" class="txt-centered label-registersuccess font-red" id="bb"></p>
      <button type="button" name="action" id="submitBtn"  onclick="testSubmit()"    value="issueInvoice">submit button</button> 
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37
0

$('#submitBtn').attr('disabled', 'disabled'); should do the trick

0

I prepared the bulletproof code for you. By default submit button will be disabled. Once you start input it will enable/disable as per value.

<html>
   <head>
       <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
   </head>
   <body>
      Input value:
      <input type="text"  id="testValue" maxlength="6" size="10">            
      <button type="button" name="action" id="submitBtn" value="issueInvoice">submit button</button> 
    <script>
        $( document ).ready(function() {
          $('#submitBtn').attr('disabled',true);
          $('#testValue').on('input propertychange paste', function() {                 
             var id=$(this).val();
             if(id < 10){                
                    $('#submitBtn').attr('disabled',true);
                }else{
                    $('#submitBtn').attr('disabled',false);                
                }
           });         

        });
    </script>
   </body>
</html>

Hope this helps!

Jenil Kanani
  • 369
  • 1
  • 14