To achieve expected result, use below option
Option 1:
As you are using .each method, using index and value you can avoid querySelectorAll, reference - http://api.jquery.com/jquery.each/
$("input.option_input").each(function(index,element){
if(element.checked){
element.checked=false;
element.dispatchEvent(new Event('change'));
element = "";
}
});
code sample - https://codepen.io/nagasai/pen/aGoMKz?editors=1010
Option 2
Option2 and preferred way is to avoid document.querySelectorAll ,as it fetches all matching elements of the DOM irrespective of the current component
Steps to achieved expected result,
- Use Renderer and ElementRef to fetch current component elements
- Use this.elem.nativeElement.querySelectorAll for fetching matching elements
component.ts
import { Component, Renderer, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular 5';
constructor(private renderer: Renderer, private elem: ElementRef){}
unsetAllOptions(){
const elements = this.elem.nativeElement.querySelectorAll('.option_input');
elements.forEach(element => {
if(element.checked){
element.checked = false
}
});
}
}
component.html
<hello name="{{ name }}"></hello>
<p>
Start editing to see some magic happen :)
</p>
<input type="checkbox" class="option_input" checked>
<input type="checkbox" class="option_input" checked>
<input type="checkbox" class="option_input" checked>
<input type="checkbox" class="option_input" checked>
<input type="checkbox" class="option_input">
<button (click)="unsetAllOptions()">UncheckAll</button>
code sample - https://stackblitz.com/edit/angular-aei58i?file=app/app.component.html