0

I would like to disable my textbox when a checkbox is checked. Problem is, I can't even disable it in the first place without any condition.

This is my HTML:

<label for="checkbox-inline">
    <input type="checkbox" class="checkbox other" name="others" value="15"
        #goodsOthersChk (change)="onCheckArray($event, Form.value.goods)">
    <p class="otherText"> Others:</p>
    <input id="goodsOthers" name="goodsOthers"
        formControlName="goodsOthers" type="text" value=""
        class="form-control otherInput" size="30%" [disabled]="isDisabled">
</label>

my component.ts:

export class GoodsComponent implements OnInit {
  isDisabled = true;
}

I don't see what I'm missing. When I inspect the textbox, it has a property of ng-reflect-is-disabled which is set to true but does not reflect on my page.

7
  • 2
    stackblitz.com/edit/angular-uazb2n it's working..... can you reproduce the issue? Commented Apr 9, 2019 at 4:39
  • @PranavCBalan i reproduced and it works as expected but it doesn't on my page Commented Apr 9, 2019 at 4:55
  • problem is related something else in your code.... share full code Commented Apr 9, 2019 at 4:57
  • 2
    I see a formControlName directive on it. The [disabled] property will not work with Reactive Forms. Use the disabled property on the FormControl in TypeScript instead. Commented Apr 9, 2019 at 5:14
  • @cristian.t is correct. You should not use the [disabled] property to disable a form control using reactive forms. Currently it gives a warning in console, later this will be deprecated and give errors. Commented Apr 9, 2019 at 5:22

1 Answer 1

1

I think that is because you are using Reactive Forms. Theoretically speaking, you can use the disabled attribute, but the Reactive Form way of doing things would be to set the disabled property when you initialise the FormGroup.

yourFrom: FormGroup = this.formBuilder.group({
  goodsOthers: [{ value: null, disabled: true }],
  .
  .
  // other Form controls
})

Other FormControl properties

Sign up to request clarification or add additional context in comments.

9 Comments

hi this works. thanks! btw, i would like to toggle between disable and enable a textbox using a checkbox. how can i do this if i have more than one textbox which i need to apply it to? i am able to do it for one but am not entirely sure how to do it for more than one
You're welcome! I see. Is each textbox 'connected' to its own individual checkbox? Or are you using this single checkbox to toggle the disabled attribute of all textboxes?
@mhfour I'll get back to you soon after my working hours
hi, i just remembered about this and was wondering if there was a way to do this?
@mhfour Sorry for the late reply. Have you thought of grouping the input and its corresponding checkbox in a FormGroup?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.