I have an app that will show a line graph with a different number of lines depending on the number of selects (1 line for each selected field, maximum of 5 selects). The first two work very well because they are the pre-defined, I can specify (change)="function". The other 3 are optional, so I have added a button that adds selects to the html. The only problem is that when I add those selects dynamically they are not assuming the onchange function which is supposed to re-draw the graphic.
Here are samples of my code:
add() {
var child = document.getElementById('addField');
var selectList = document.createElement("select");
if (child.previousElementSibling) {
selectList.id = "field" + (parseInt(child.previousElementSibling.id.charAt(child.previousElementSibling.id.length-1)) + 1);
selectList.innerHTML = selectList.innerHTML + child.previousElementSibling.innerHTML;
}
else {
selectList.id = "field" + (parseInt(child.previousElementSibling.id.charAt(child.previousElementSibling.id.length-1)) + 1);
selectList.innerHTML = selectList.innerHTML + child.previousElementSibling.innerHTML;
}
if(parseInt(selectList.id.charAt(selectList.id.length-1)) > 5)
alert("Maximum number of parameters is : 5");
else {
selectList.setAttribute('onchange', "drawChart()");
child.parentNode.insertBefore(selectList, child);
}
}
This function is to add the select above the addField button and it says that drawChart doesn't exist.
The html page looks like this:
<div id="principal">
<label>Parameters :</label>
<select id="field1" (change)="drawChart()">
<option *ngFor="let pos of array"
[value] = "pos.name"
[selected]="pos.name == 'def'">
{{pos.name}}
</option>
</select>
<select id="field2" (change)="drawChart()">
<option *ngFor="let pos of array"
[value] = "pos.name"
[selected]="pos.name == 'def2'">
{{pos.name}}
</option>
</select>
<input type="button" class="button" id="addField" value="Add Field" (click)="add()"/>
</div>