binar
binar

Reputation: 356

Disable input if checkbox checked

i want to disable inputs when a checbox is checked.

part of html:

 <div class="span1 ">
  <div class="control-group" id="niedziela">
    <label class="control-label" for="">Niedziela</label>
    <div class="controls">
      <input type="text" value="<?= $metavalue->nightlife_open_7_start ?>" name="meta[nightlife_open_7_start]" id="nightlife_open_7_start" class="m-wrap span12 timepicker-24" placeholder="">
      <input type="text" value="<?= $metavalue->nightlife_open_7_end ?>" name="meta[nightlife_open_7_end]" id="nightlife_open_7_end" class="m-wrap span12 timepicker-24" placeholder="">
      <input id="klient_open_4_end" type="checkbox" <?=_ set_checked($metavalue->nightlife_open_7_end,'do ostatniego klienta') ?> value="do ostatniego klienta" name="meta[nightlife_open_7_end]" class="m-wrap span12 timepicker" placeholder=""> Do ost. klienta
      <input id="zamkniete_open_4_end" type="checkbox" <?=_ set_checked($metavalue->nightlife_open_7_start,'zamkniete') ?> value="zamkniete" name="meta[nightlife_open_7_start]" class="m-wrap span12 timepicker" placeholder=""> Zamknięte
    </div>
  </div>
</div>

what i want is that if checkbox with id klient_open_4_end is checked it should disable input with id="nightlife_open_7_end"

if checkbox with id="zamkniete_open_4_end" is checked should disable both inputs text areas

i tried with :

<script type="text/javascript">
  $(document).ready(function($) {
    $('#klient_open_4_end').change(function() {
      $(this).find("#nightlife_open_7_start").attr("disabled", true);
    });
  });
</script>

Upvotes: 2

Views: 16932

Answers (5)

Syed Ali
Syed Ali

Reputation: 161

It is for onclick disable and enable

<!DOCTYPE html>
<html>
<body>

Checkbox: <input type="checkbox" id="myCheck">

<button onclick="check()">Check Checkbox</button>
<button onclick="uncheck()">Uncheck Checkbox</button>
<script type="text/javascript">
  function check() {
    if(document.getElementById("myCheck").checked == true)
    {
    document.getElementById("input").disabled = true;
    }
    else{
     document.getElementById("input").disabled = false;
    }
}
</script>

</body>
</html>

Upvotes: 1

asprin
asprin

Reputation: 9833

You'll need to listen to the click event of the checkboxes. Since you've tagged the question with jQuery too,

$(function(){
   $('#klient_open_4_end').on('click', function(){           
       if($(this).is(':checked')){
           $('#nightlife_open_7_start').attr('disabled', true);
       } else {
           $('#nightlife_open_7_start').attr('disabled', false);
       }
   });

   $('#zamkniete_open_4_end').on('click', function(){
       // assuming the textarea is inside <div class="controls /">
       if($(this).is(':checked')){
           $('.controls input:text, .controls textarea').attr('disabled', true);
       } else {
           $('.controls input:text, .controls textarea').attr('disabled', false);
        }
   });
});

Update

You can shorten this even more and use the following instead:

$(function(){
   $('#klient_open_4_end').on('click', function(){                  
      $('#nightlife_open_7_start').attr('disabled', $(this).is(':checked'));       
   });

   $('#zamkniete_open_4_end').on('click', function(){
       // assuming the textarea is inside <div class="controls /">
       $('.controls input:text, .controls textarea').attr('disabled', $(this).is(':checked'));       
   });
});

Upvotes: 6

Akshay Khale
Akshay Khale

Reputation: 8371

checkout this simple code

<html>
    <body>
        <input type="checkbox" id="klient_open_4_end" name="klient_open_4_end"/>
        <input type="text" id="nightlife_open_7_end" name="nightlife_open_7_end"/>
    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#klient_open_4_end").change(function(){
                if($(this).prop("checked")){
                    $("#nightlife_open_7_end").prop("disabled",true);
                }
                else{
                    $("#nightlife_open_7_end").prop("disabled",false);
                }
            });
        });
    </script>
</html>

In above code

$("#nightlife_open_7_end").prop("disabled",true/false);

you may use

$('#nightlife_open_7_end').attr('disabled', true/false);

Upvotes: 0

Andrew Aitken
Andrew Aitken

Reputation: 681

This is fairly simple, you can do it like this

$(document).ready(function () {
    $('#klient_open_4_end').click(function () {
        $('#nightlife_open_7_start').prop('disabled', function(i, v) { return !v; });
    });
});

But from your snippet I'm assuming you want to run something when the page loads to disable the input as well, you can do that the same way essentially.

Upvotes: 3

Tushar Gupta
Tushar Gupta

Reputation: 15933

You can achieve this as

$('#chk').change(function(){
  if($(this).is(':checked'))    
    $('#inp1').prop('disabled','disabled')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='text' id='inp1' />
<input type='text' id='inp2' />
<input type='checkbox' id='chk' />

Upvotes: 0

Related Questions