5

I am trying to create a small reusable function that will, when checked, disable a text field and insert a default value of '160', and when unchecked, enable the field and remove the value. I have it mostly finished but the unchecking part is throwing me off.

$('#chkIsTeamLead').change(function(){


   if ($('#chkIsTeamLead').checked = true){
      $('#txtNumHours').val('160').attr('disabled', 'disabled');
      console.log('checked');
   }

  if ($('#chkIsTeamLead').checked = false){
     $('#txtNumHours').val('').removeAttr('disabled');
     console.log('unchecked');
   }

});

I had it setup as a reusable function with arguments passed to it but that was giving me more trouble, I would like the arguments to be checkbox, target, and value preferably link to my current code: http://codepen.io/AlexBezuska/pen/bwgsA Thanks for any help you can provide!

5
  • 1
    Use $('#chkIsTeamLead').is(':checked') in your conditions. Commented May 29, 2013 at 6:23
  • And you must compare two values with ==. Commented May 29, 2013 at 6:24
  • 3
    And you can use if(){} else{} rather than 2 if's Commented May 29, 2013 at 6:25
  • Check this jsFiddle. Commented May 29, 2013 at 6:25
  • I had it setup as a reusable function with arguments passed to it but that was giving me more trouble What trouble ? Can you post that code where you tried to pass arguments as well ? Commented May 29, 2013 at 6:30

2 Answers 2

14

Working jsFiddle Demo

  1. Use .is(':checked').
  2. You must compare two values with ==.
  3. When you are working with attribute like disabled, it's better to use .prop() method instead of .attr().

$('#chkIsTeamLead').change(function(){
    if ($('#chkIsTeamLead').is(':checked') == true){
        $('#txtNumHours').val('160').prop('disabled', true);
        console.log('checked');
    } else {
        $('#txtNumHours').val('').prop('disabled', false);
        console.log('unchecked');
    }
});

References:

  • .attr() - jQuery API Documentation
  • .prop() - jQuery API Documentation
  • .is() - jQuery API Documentation
Sign up to request clarification or add additional context in comments.

5 Comments

I think ==true isnt necessary as if will work that out by itself
@passionateCoder Yes, I just left it there for == to show OP.
also , Doug Crockford would kill you if you didnt use === instead of == :P
@passionateCoder Yesssssss, I love him ;).
This works great, thanks, and thanks for the direction on .prop(), I will have to read this over and learn more about it.
1

try this

$('#chkIsTeamLead').change(function(){


   if ($('#chkIsTeamLead').is(':checked')){
      $('#txtNumHours').val('160').attr('disabled', 'disabled');
      console.log('checked');
   } else {
     $('#txtNumHours').val('');
    $('#txtNumHours').removeAttr('disabled');
     console.log('unchecked');
   }
 });

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.