I’m new to html and recognize this may be a basic question. However I haven’t been able to find any examples of this sort of functionality yet. I’m trying to do math with the input from two different sliders. Thus far, my code looks like this:
function f(x, y) {
return x + y
}
function sliderx(value) {
val = f(value, 1.0)
console.log(val)
}
<h1> Slider Test</h1>
<div id="myDiv"></div>
<p> sliderx </p>
<form oninput="amount.value=rangeInput.value">
<input type="range" id="rangeInput" name="rangeInput" step="0.05" min="0.0" max="1.0" value="get_min() " oninput="sliderx(this.value)">
<output name="amount" for="rangeInput"></output>
</form>
<p> slidery </p>
<form oninput="amount.value=rangeInput.value">
<input type="range" id="rangeInput" name="rangeInput" step="0.05" min="0.0" max="1.0" value="get_min() " oninput="slidery(this.value)">
<output name="amount" for="rangeInput"></output>
</form>
I created two sliders (sliderx and slidery) and defined f(x,y) = x+y. My goal is to be able to print x+y to console.log. I can call silderx and my console updates accordingly for fixed y. However, I don’t see any good way to update y using slidery, while keeping x defined by sliderx. Is there any way to do this?
2
Answers
Yes, there is a solution for your problem!
To achieve your goal, you can define two global variables x and y to store the current values of the sliders, and update them whenever the slider values change. Then, you can calculate `f(x,y)` using the updated values of x and y, and log the result to the console. Also as your comments also mentioned IDs should be unique so stick with unique IDs and names for your elements. See:
In this implementation, we define two global variables x and y to store the current values of the sliders. Whenever a slider value changes, the update function is called, which
updates the values of x and y
, calculatesf(x,y)
, and logs the result to the console. The update function is called whenever either slider value changes, using theoninput
attribute of the slider inputs. Theoutput
elements are associated with their corresponding slider inputs using thefor
attribute.