Edit: altered example code to include relevant typescript
I have a form with different buttons for the user to select. The button to submit the form is disabled until the user selects a button. The trick here is that I would like one of the options to not enable the submission button, but to instead show a textbox. The button should then be enabled if the user enters something into the textbox. At the moment, my HTML looks a little something like this:
<h1>Do you like sandwiches?</h1>
<form id="my-form" action="onSubmit()">
<div>
<button *ngFor="let option of myOptions"
class="form-button"
(click)="selectThisButton(option)">{{ option }}</button>
<div id="other-input" *ngIf="showInput">
<label for="text-input">Please elaborate.</label>
<input id="text-input" type="text" required>
</div>
</div>
</form>
<!--...some other code...-->
<button id="submit-button" form="my-form" type="submit" disabled="!my-form.valid">Next</button>
The typescript class is fairly straightforward as well:
selectedOption: string
showInput = false
needsExplanation = false
myOptions = ["Yes", "No", "I don't understand", "Other"]
selectThisButton(option: string){
this.selectedOption = option
this.needsExplanation = (option === "I don't understand")
this.showInput = (option === "Other")
}
onSubmit(){
if(this.selectedOption === "Other"){
this.selectedOption = (document.getElementById("text-input") as HTMLInputElement).value
}
continueAndDoOtherStuff()
}
All of this works except for the last step, i.e. entering something into the textbox doesn't enable the "next" button when it should.
I've also tried not using a form and just putting an EventListener on the textbox after the ngIf has rendered to listen for a keyup event, but that didn't work either.
Any help would be appreciated, especially suggestions that don't rely on jQuery.