0

I have a textbox and a checkbox, when the checkbox is unchecked, I need to disabled the textbox and when the user selects the checkbox, I want to re-enable the textbox.

I tried this:

ASP.NET code:

<div class="checkConfiguration">
    <asp:CheckBox runat="server" ID="stopGeneralNumber" Text="General Number"   CssClass="cbStopReason" Checked="false" />
    <asp:TextBox runat="server" Enabled="false"></asp:TextBox>
</div>

jQuery code

 $('.cbStopReason').on('click', function () {
        if ($(this).attr('checked')) {
            alert("now checked");
            $(this).nextAll("input").removeAttr("disabled");
        } else {
            alert("now un checked");
            $(this).nextAll("input").attr("disabled", "disabled"); 
        }
    })

The jQuery code is already in document ready function, but the problem is that my code works good to disable the textbox, but it doesn't work to re-enable it, what wrong did I do please?

Update 1: This is the actual HTML that is being generated

 <div class="configurationData">
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopUrgentNumber" type="checkbox" name="stopUrgentNumber"><label for="stopUrgentNumber">Urgent Number</label></span>
                                <input name="ctl17" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopNurseNurse" type="checkbox" name="stopNurseNurse"><label for="stopNurseNurse">Nurse Number</label></span>
                                <input name="ctl18" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopScheduledNumber" type="checkbox" name="stopScheduledNumber"><label for="stopScheduledNumber">Scheduled Number</label></span>
                                <input name="ctl19" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopClockNumber" type="checkbox" name="stopClockNumber"><label for="stopClockNumber">Clock Number</label></span>
                                <input name="ctl20" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopLastCheckNumber" type="checkbox" name="stopLastCheckNumber"><label for="stopLastCheckNumber">Last Check Number</label></span>
                                <input name="ctl21" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopArrivalNumber" type="checkbox" name="stopArrivalNumber"><label for="stopArrivalNumber">Arrival Number</label></span>
                                <input name="ctl22" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopGeneralNumber" type="checkbox" name="stopGeneralNumber"><label for="stopGeneralNumber">General Number</label></span>
                                <input name="ctl23" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                        </div>
7
  • Have you tried using $(this).prop('checked') instead? Commented May 5, 2015 at 22:51
  • @BryanDowning i just tried your suggestion, i put your code in the if statement and I still have the same problem that is described on the question Commented May 5, 2015 at 22:52
  • Seems to work for me: jsfiddle.net/bryandowning/wcktqdnt Commented May 5, 2015 at 22:57
  • @BryanDowning but you have used a different html that mine, i updated the question and added the generated html Commented May 5, 2015 at 23:01
  • This may help: jsfiddle.net/SPddm stackoverflow.com/questions/13831601/… Commented May 5, 2015 at 23:05

4 Answers 4

2

According to your OP, it looks like your binding on your span. Changed it to bind on your checkbox. Also changed the bind from click to change. http://jsfiddle.net/0fL0pumf/1/

$('#stopGeneralNumber').on('change', function () {
    var $this = $(this);

    if ($this.is(':checked')) {
        $this.parent().nextAll("input").prop("disabled", false);
    } else {
        $this.parent().nextAll("input").prop("disabled", true);
    }
});

Reduced the javascript...

$('#stopGeneralNumber').on('change', function () {
    $(this).parent().nextAll("input").prop("disabled", !$(this).is(':checked'));
});

Generic to use the class, but not the id of the checkbox. Just uses a different selector.

$('.cbStopReason > input[type=checkbox]').on('change', function () {
    var $this = $(this);
    $this.parent().nextAll("input").prop("disabled", !$this.is(':checked'));
});
Sign up to request clarification or add additional context in comments.

2 Comments

i can't use the id, i must use the class because i have too many check box and i want to treat them all using the class, i just updated my question to give u the five checkbox that i have
i tried your code, and i still face the same proboem, i am thinking maybe because you tried your code on just one checkbox while i have fife of them, i updated the quesiotn to give u my generated html
0

In the rendered code, you no longer have a checkbox with the class cbStopReason -- that class is applied to the span and the span will never be checked.

One fix is to change the conditional to:

$(this).find("input").attr('checked')

4 Comments

could you give me a working jsfiddle? or at least tell me what would my code look like for the true and false condisions
jsfiddle.net/9vLtpqtt/5 simply add .find("input") before the .attr in your if
the code you put is not working on my machine though it is working good on jsfiddle, i can't find the problem, i gave u the code, how can it works on jsffidle but not on my machine please?
I tried to do this alert($(this).find("input").attr('checked')); before the if statement, and i kept getting undifinied printed
0

You're binding the event handler to the wrong element (selector). Also, you can reduce your code quite a lot.

I would do it like this:

$(function () {
    // On the checkbox click.
    // Here I would give these checkboxes a particular class
    // to use on the selector.
    $('.checkConfiguration input[type=checkbox]').on('click', function () {
        // Go to its parent and look for the input.
        // Enable/disable it according to the checkbox 
        // checked value.
        $(this).closest('.checkConfiguration')
            .find('input[type=text]')
            .prop('disabled', !$(this).is(':checked'));
    });
});

Demo

Comments

-1

Use if ($(this).is(':checked')) instead.

3 Comments

I already did that before asking the question here, and I still have the same problem that is described on the question
But does the browser shows the alert when you activate the checkbox?
the alert is always the one that is written in the false part

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.