1

I am using template driven form. Currently i am getting the true or false value. but i need the actual value of checkbox

 <form #f = "ngForm" (ngSubmit) = "onClickSubmit(f.value)">
    <h2 class="no-top-space">
        {{Questions?.Question}}
    </h2>
    <div *ngFor="let op of options">
        <input type="checkbox" name="check" [value]="option.Value" ngModel>{{op.Value}}
    </div>
    <button type="submit" role="button" class="btn btn-primary blue">Submit</button>
</form>



onClickSubmit(data){
    console.log("dataaa",data)
}
1

1 Answer 1

1

Try this: In html:

 <input type="checkbox" name="check" [value]="op.Value" ngModel (change)="handleChange($event)">{{op.Value}}

In ts:

declare an array variable which will have a blank array initially. like

selectedOptions:string[]=[] 

Then build the handleChange method as:

handleChange(event:any)
{
    if(this.selectedOptions.length>0)
    {
     let existedOption = this.selectedHobby.findIndex((x) => x === e.target.value); //to check if it is already present in the array or not
          if (existedOption != -1)
           {
                this.selectedOptions.splice(existedOption, 1);
           } 
           else 
           {
              this.selectedOptions.push(e.target.value);
           }
    } 
    else 
    {
          this.selectedOptions.push(e.target.value);
    }
    console.log(this.selectedOptions);
  }
Sign up to request clarification or add additional context in comments.

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.