I want to be able to use the values that I get from my sliders here in more than just one action for some later calculation. In addition to the solution below, I tried declaring space as a variable before I excecute the jQuery function, but that didn’t work either. I have the following code:
$("#spaceRange").on("input", function () {
space = Number($(this).val());
leftEar.x(space * scale + faceDist);
rightEar.x(-(space * scale + faceDist));
updateObjects();
return space;
});
var num = $("#spaceInput").val();
console.log(num);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col">
<label for="rightLeftSlider" class="form-label">R/L Space - Inches</label>
<form>
<input type="range" class="form-range w-75" id="spaceRange" min="0" max="20" value="0"
oninput="this.form.spaceInput.value=this.value" />
<input type="number" id="spaceInput" min="0" max="20" value="0"
oninput="this.form.spaceRange.value=this.value" />
</form>
</div>
2
Answers
You are going to need to put references to them in some sort of function that is triggered and called after they have been changed.
Having variables just floating in your code that load when the page loads isn’t storing the values of the fields after they have been changed. It’s storing the values at that point in time.
Here is an example that shows the values inside of a function, in this case just a click handler on a random button. Also I have a reference just floating outside that loads when the page loads.
Not 100% sure on your direction here but this might get you started on how to pass variables around.