oTree Forum >

Extract form_field value from choices

#1 by martin

Hi, I am creating a JavaScript to return a value dependent on the value inputed in a formfield among several choices. I can succesfully create the JavaScript process, but am failing to extract the formfield value to use as an input.

For example:
#Models
class Player(BasePlayer):
    number = model.IntegerField(choices=[1,2])
    
#Page
{{ block content }}
<table>
<tr>
<td id="input">{{ formfield 'number' }}</td>
<td>+1:</td>
<td>XXXX</td>
</tr>
</table>
{{ endblock }}    

I want XXXX to display 2 (1+1) when formfield 'number' is 1, and 3 (2+1) when formfield 'number' is 2. My problem is that I cannot access the values of formfield 'number' in my .js

Thanks for any help
 
Martín

#2 by Victor

Hi,

Just a thought based on how I managed to do this myself in the past; When you test your code, use right-click + "inspect" to see how the "formfield" feature translates into actual HTML code. Here you can also see the ID given to it. You can use JS to extract this number and present it somewhere else as it is updated on input or on change. You would also have to give the place where you want to present it an ID. 

Hope this helps.

#3 by Chris_oTree

See input_calculation in otree-snippets.

#4 by martin

Thanks for the suggestions!

The code in input_calculation did the trick

Write a reply

Set forum username