My goal is to be able to dynamically add range inputs onto a web page via javascript. I am using the following code for the sliders
<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
<p>Value: <span id="demo"></span></p>
</div>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
}
</script>
https://www.w3schools.com/howto/howto_js_rangeslider.asp
I have tried using innerHtml, document.createElement("input"), and templates to achieve this. I am able to draw the slider however I am unable to get the value to print and show any help would be appreciated on how to achieve this.
This is my most recent attempt
Script
function showContent() {
var temp = document.getElementsByTagName("template")[0];
var clon = temp.content.cloneNode(true);
document.body.appendChild(clon);
}
Template.myTemplate.rendered = function(){
document.getElementById("slider").oninput = function() {
myFunction()
};
function myFunction() {
var val = document.getElementById("slider").value //gets the oninput value
document.getElementById('output').innerHTML = val //displays this value to the html page
console.log(val)
}
</script>
<template name="myTemplate">
<input id="slider" type="range" min="50" max="100" step="10" value="50">
<output id="output">abc</output>
</template>
<button class = "button" onclick="showContent()">Copy text</button>
I also tried this thanks to MMDM which resulted in the 3 ranges drawing and 3 values being shown however, they do not update.
<script>
window.onload = function()
{
var titles = ["demo1", "demo2", "demo3"];
for(var i = 0; i < 3; i++)
{
var container = document.getElementsByClassName('slidecontainer')[0];
var slider = document.createElement("input");
slider.type = 'range';
slider.value = 50;
container.prepend(slider);
container.innerHTML += ("<p>Value: <span id=" + titles[i] +"></span></p>")
var output = document.getElementById(titles[i]);
output.innerHTML = slider.value;
slider.oninput = function() {
output.innerHTML = this.value;
}
}
}
</script>
<div class="slidecontainer">
</div>