0

I have a button which looks like an on off Button Button looks as

Button is by default on but when user will click it should get off and when it is clicked it should disable the input field my button is as

Button

<input type="checkbox" class="make-switch" id="on_off" name="double_optin" checked data-on-text="on" data-off-text="off">

Input field which i want to be disabled if above button clicks

<div class="col-md-4 FullName">
 <input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : '' }}" required />
 </div>

My Jquery code is as

$('#on_off').click(function(){
    $('.FullName').prop('disabled', true);
});

I took above help from Disable Input field

Help Please its not working for me

Community
  • 1
  • 1
Ramzan Mahmood
  • 1,881
  • 2
  • 21
  • 46

4 Answers4

2

First of your code is missing something. Like the control with class FullName

You can get the value of the checkbox by using $('#on_off').is(':checked') or in this case $(this).is(':checked') because we are using $('#on_off') click event.

Since you dont have a "Fullname" class anywhere in your example I added email to the Id field of the input

$('#on_off').click(function() {

  $('.FullName [name="email"]').attr('disabled', $(this).is(':checked') ? false : true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="make-switch" checked id="on_off" name="double_optin" data-on-text="on" data-off-text="off">

<div class="col-md-4 FullName">
  <input type="text" class="form-control" name="email" value="disable me by click on checkbox" required />
</div>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
0

Please try this code.

Here is your updated code:

<input type="checkbox" class="make-switch" id="on_off" name="double_optin" checked data-on-text="on" data-off-text="off">
<div class="col-md-4 FullName">
 <input type="text" class="form-control" name="email" value="{{ isset($student->email) ? $student->email : '' }}" id= "email"required />
 </div>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $('#on_off').change(function() {
        //alert();
        if ($(this).prop('checked')) {
            $('.FullName input').prop('disabled', false);
        }
        else {
            $('.FullName input').prop('disabled', true);
        }
    });
});

</script>

Hope, this may help you.

Prateek Verma
  • 869
  • 1
  • 6
  • 9
0

use .attr('disabled',true) or .removeAttr('disabled') instead of .prop('disabled', true); if not working.

flo lab
  • 19
  • 2
0

This is the easiest you can get.

Create a checkbox. In click event of checkbox, pass its state as a boolean to buttons disabled value.

Though I would suggest to use ready made toggle button. Link is here

$(document).ready(function(){
  $(":checkbox").change(function(){
    $(":button").prop("disabled",$(this).is(":checked"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type=checkbox />
<input type=button value=Target />
Hemal
  • 3,682
  • 1
  • 23
  • 54