0

I need to disable the button when no value is inserted in the input text

<form>
    <div class="form-group">             
        <p class="description">Please Enter Your Tracking Number.</p>

        <input type="text" id="tracking-input" class="input-text full-width" placeholder="Enter your Tracking number(s)"onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
    </div> 
    <br>
    <button type="submit" id="register" class="full-width btn-medium">Track</button>
</form>

Have tried this but doesn't work

<script>

function buttonState(){
    $("input").each(function(){
        $('#register').attr('disabled', 'disabled');
            if($(this).val() == "" ) return false;
        $('#register').attr('disabled', '');
    })
}

$(function(){
    $('#register').attr('disabled', 'disabled');
    $('input').change(buttonState);
})

</script>
Joe B.
  • 1,124
  • 7
  • 14
  • maybe this will help https://stackoverflow.com/questions/7067005/disable-button-whenever-a-text-field-is-empty-dynamically – usrNotFound Aug 01 '17 at 01:59
  • Possible duplicate of [jQuery disable/enable submit button](https://stackoverflow.com/questions/1594952/jquery-disable-enable-submit-button) – sokin Aug 01 '17 at 01:59
  • The mistake that im using – Mohamed Khader Aug 01 '17 at 02:01
  • This doesn't work as well $(document).ready(function(){ $('button[type="submit"]').attr('disabled','disabled'); $('input[type="text"]').change(function(){ if($(this).val != ''){ $('button[type="submit"]').removeAttr('disabled'); } }); }); – Mohamed Khader Aug 01 '17 at 02:07

2 Answers2

1

You can use keyup event to check for disabled property of button based on the value

$(document).ready(function(){
  $('#register').prop('disabled',true);
    $('#tracking-input').keyup(function(){
        $('#register').prop('disabled', this.value === "");     
    })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <div class="form-group">
      <p class="description">Please Enter Your Tracking Number.</p>
      <input type="text" id="tracking-input" class="input-text full-width" placeholder="Enter your Tracking number(s)"onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
  </div> <br>
  <button type="submit" id="register" class="full-width btn-medium">Track</button>
</form>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

$("#register").prop("disabled",true);

$("#tracking-input").keyup(function(){
var valuee=$("#tracking-input").val().trim();
if(valuee==""){
$("#register").prop("disabled",true);

}else{
$("#register").prop("disabled",false);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
                <div class="form-group">

                    <p class="description">Please Enter Your Tracking Number.</p>

                    <input type="text" id="tracking-input" class="input-text full-width" placeholder="Enter your Tracking number(s)">
                </div> <br>
                <button type="submit" id="register" class="full-width btn-medium">Track</button>
            </form>
K-Square
  • 63
  • 5