2

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>
2
  • show us the rendered html. Commented Jul 22, 2015 at 9:24
  • give a working jsFiddle example to show your problem Commented Jul 22, 2015 at 9:26

5 Answers 5

6

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'));       
   });
});
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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>

Comments

0

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' />

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.