oTree Forum >

Value doesn't save as formfield

#1 by sdf


I've tried everything to save a value from a field ('prediction') using JS, but it just won't save. Instead, I get an error saying the value is not valid, when trying to click "next" on that page. Note: I manage to display the 'prediction' value in the highchart, using this line in my JS: let input = document.getElementById('prediction');. What can I do to save this formfield? Any help is greatly appreciated!

from my html:

<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
<label for="prediction">Put in your value here for your prediction:</label>
<input type="number" id="prediction">

#2 by Chris_oTree

Add name="prediction" to the input

#3 by sdf

This still gives me the same error "Please select a valid value. The two nearest valid values are..."

#4 by Chris_oTree

Where is the code that puts a value into this field? i.e. setting input.value = 42

#5 by sdf

This is probably what I'm missing. 

The relevant JS code: 

let input = document.getElementById('prediction');

input.addEventListener('input', function() {
  let amountInput = input.value;
  data[3].subject_forecast = amountInput;

#6 by Chris_oTree

this code doesn't write anything to the input, rather it reads from the input and stores it somewhere else (the 'data' variable).

where does the value come from? the user enters a value in the input manually?

#7 by Chris_oTree

also that error could be that a number input expects an integer unless you specify a step= attribute, e.g. step="any" or step="0.01". that's why it says 'the nearest valid values are ...."

#8 by sdf

I can't believe it, but this really was the issue in the end. Adding step="0.01" did the trick. 

Thanks so much, Chris!

Write a reply

Set forum username