5

I want to disable a textbox if a user unchecks a checkbox and change the background colour of textbox. If the user checks the checkbox then that textbox should be editable and changes to white colour. Here is the code

$(document).ready(function () {
    $(".ba").click(function () {
        $(".tex").css("background-color", "#CCCCCC");
    });
});

Where .ba is the checkbox and .tex is textbox classes, now when the user clicks it changes to grey color but if they click again the color not changing.I want to see if the check box is checked if it is checked then nothing should happen else if the user uncheck the check box the textbox background color should change and it should go to non-editable that is disabled.Can anyone help?

I am using Below javascript to check all and uncheck all

function checkAll(formname, checktoggle) {
    var checkboxes = new Array(); 
    checkboxes = document[formname].getElementsByTagName('input');

    for (var i=0; i<checkboxes.length; i++)  {
        if (checkboxes[i].type == 'checkbox')   {
            checkboxes[i].checked = checktoggle;
        }
    }
}

Is this causes any problem?

4 Answers 4

6

Working jsFiddle Demo

Consider the following markup:

<input type="checkbox" class="ba" checked="checked" />
<input type="text" class="tex" />

And your JS:

$(function () {
    $('.ba').on('change', function () {
        var checked = $(this).prop('checked');
        $('.tex').prop('disabled', !checked);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

@raj Did you check the FIDDLE demo? Which versin of jQuery are you using?
Why do you capitalize jQuery correctly but spell and capitalize jsFiddle wrongly? And consistently wrongly, at that.
@BoltClock OK, you are exactly right, I'll keep it in mind ;).
3

Here is JS the code

$(".ba").click(function () {
    if ($(".ba").is(":checked")) {
        $(".tex")
            .removeAttr("disabled")
            .css("background-color", "white");
    }
    else {
        $(".tex")
            .attr("disabled", "disabled")
            .css("background-color", "red");
    }
});

I recommend you to check out Jquery API. It's a fun read :)

3 Comments

how can i do this to all checkbox with different class.can i use this function again and again?
do you mean that you have multiple checkboxes that each should enable/disable a textbox?... sorry for being late
Ya i used the same function for all.It's all working.Only thing is if i uncheck all then all the check box are unchecking but not disabling because i used another javascript function(Shown in my question) to do this.
1
$(document).ready(function(){
$(".ba").click(function() {
if ($('#check_id').is(":checked"))
{
  $(".tex").css("background-color","#CCCCCC");
}
else
{
   $(".tex").css("background-color","#DDDDDD");
}
});
});

Comments

1

Try like

$(".ba").on('click',function() {
   if($(this).is(':checked'))
       $(".tex").css("background-color","#CCCCCC");
   else
       $('.tex').prop('disabled', false);
})'

1 Comment

working for one time only,if i unchecks(click again) text background color not changing to white.

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.