You are querying the document for the fieldset
, but you have two of those. Introduce id
attribute to your fieldsets, then do the same thing for each fieldset.
Put id
s to array: indexes: string[] = ['fieldset1', 'fieldset2'];
and add to each fieldset
their id
attribute, like <fieldset [id]="indexes[0]">
and <fieldset [id]="indexes[1]">
, then change your ngAfterViewInit()
method to iterate over indexes
array, using each value for getElementById
selector:
ngAfterViewInit() {
this.indexes.forEach((id) => {
const fieldset = document.getElementById(id);
const content = fieldset.querySelector('.content');
fieldset.querySelector('legend').addEventListener('click', function () {
content.classList.toggle('hidden');
});
content.classList.remove('hidden');
});
}
Your edited stackblitz code.