I want to loop over my function SliderComponent, with different inputs, inside the Html in order to create several components. I found this solution online where we build string and return as Html, using "id" and "getElementById" but I can't make it work.
export function GrommetButtonEx() {
return (
<div>
<Grommet theme={grommetTheme}>
<Sidebar
border={{ color: "grey", size: "medium" }}
width="340px"
background={{ color: "black", opacity: "strong" }}
style={{ borderRadius: "15px" }}
>
<Accordion style={{ color: "grey" }}>
<AccordionPanel
style={{ height: "30px" }}
label={
<Text color="white" weight="bold" size="small">
Joint Position
</Text>
}
>
<div id="output_div"></div>
{/* This works
<SliderComponent
sliderName={"slider 5"}
min={0}
max={17}
step={0.1}
/>
*/}
</AccordionPanel>
</Accordion>
</Sidebar>
</Grommet>
</div>
);
}
window.onload = function () {
var outputHTML = "";
for (var k = 0; k < 5; k++) {
outputHTML +=
'<SliderComponent sliderName={"slider ' +
k +
'"} min={0} max={17} step={0.1}';
}
document.getElementById("output_div").innerHTML = outputHTML;
};
I got the following error for:
TypeError: Cannot set properties of null (setting 'innerHTML')
at:
document.getElementById("output_div").innerHTML = outputHTML;
Is this the right way to do it, or is there a better way?