How find empty input value in jquery

I have two checkboxes the one checkbox value is 1 and the second one is 0. One checkbox is for enabling and other for disabling. Now I want to identify when a user clicks on Disable checkbox How I will get Disable checkbox value and if a user clicks on Enable box then how I will get Enable Checkbox value

<input type="radio" name="forgot_pass" class="forgot_pass_enable" value="1" checked>

<input type="radio" name="forgot_pass" class="forgot_pass_disable" value="0">

I have tried This code but it's not working for me any help

$("body").on('change', '.forgot_pass_enable', function(event) 
{
  event.preventDefault();
  /* Act on the event */  

  if($('.forgot_pass_enable').val().length > 0) 
  {
    var status = 1;

    console.log(status);
  }

  if($('.forgot_pass_disable').val().length > 0)
  {
    var status  = 0;

    console.log(statusasdad);
  }
});

1 answer

  • answered 2018-01-14 12:43 Rory McCrossan

    Neither of the elements you've shown are checkboxes - they are radio inputs.

    To get the selected value apply the same class to both elements, then hook the change event to that class. Then you can simply read the value from it, like this:

    $("body").on('change', '.forgot_pass', function(e) {
      e.preventDefault();
      var status = this.value;
      console.log(this.value === '1' ? 'enabled' : 'disabled', status);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <input type="radio" name="forgot_pass" class="forgot_pass" value="1" checked="checked">
    
    <input type="radio" name="forgot_pass" class="forgot_pass" value="0">

    If you did want to use a checkbox for this, which would be more applicable for an enable/disable switch, then the method is the same except you look for the checked property instead:

    $("body").on('change', '.forgot_pass', function(e) {
      e.preventDefault();
      var status = this.checked ? '1' : '0'
      console.log(status);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <input type="checkbox" name="forgot_pass" class="forgot_pass" value="1" checked="checked">