-1

I have a checkbox group with 3 items. I created it with Angular 5.0.5 and Clarity vm. I want my function to check if at least one of the checkboxes is checked and THEN go at the next page, which is a wizard. Until now the wizard opens without checking if at least one checkbox is selected.

My html looks like this:

<table class="table">
<tbody>
<tr *ngFor="let item of items">
<td>
<input  type="checkbox" name="allowNext" [(ngModel)]="item.chosen">
</td>
<td>
{{item.name}}
</td>
</tr>
</tbody>
</table>
<br>
</form>
<button [disabled] class="btn btn-primary" (click)="go()"> Next </button>

my Typescript for it looks like this:

items= [
    {name: 'checkbox1', chosen: false},
    {name: 'checkbox2', chosen: false},
    {name: 'checkbox3', chosen: false}
  ];
 go() {
 if(this.items.chosen === true) {
 this.wizard.open();
 ngOnInit () {}
 constructor( public router: Router) {}

could you guys please help me define the function correclty?

Init
  • 15
  • 1
  • 4
  • you have to check if every select is checked. You can use a loop or map and reduce from Arrays. – Fals Feb 19 '18 at 15:20

2 Answers2

2
this.items.some(i => i.chosen) 

will check if at least one item is chosen

Safiyya
  • 1,383
  • 1
  • 10
  • 16
-1

With JQuery you can simple do this :

if ($('#frmTest input:checked').length > 0 ) {

  //your code

}
Léo R.
  • 2,620
  • 1
  • 10
  • 22
  • Thanks, but I need it to work with Typescript/ Angular . I tried if ((this.checked).length > 0 ) { this.wizard.open(); } but it didnt work – Init Feb 19 '18 at 15:19