oTree Forum >

Update Variable on HTML

#1 by PatrickT

Hey everyone. First of all, I appreciate all the assistance I have received on the forums. I am finally nearing the end of my coding, knock on wood.

I have a pretty simple question. It appears as though the variables I show through HTML are set at the beginning of the page and never update. For example I have the following text which appears when a player accepts a wage:

<div id="wage_accepted" style="display: none">
        You have accepted a wage of {{player.wage_paid}} ECUs for the period. Please wait for the wage auction to complete. <br>       
    </div>
  
It says that "You have accepted a wage of 0 ECUs for the period. Please wait for the wage auction to complete." But I know from my console that player.wage_paid=50 for this player. The variable starts at 0, so I presume it is set at the start of the page and never updated. So, how can I get the variables on the page to update? Thank you all once again for your help.

Best,
PatrickT

#2 by BonnEconLab

Enclose the amount in a <span>, such as

   You have accepted a wage of <span id="wage-paid">{{player.wage_paid}}</span> ECUs for the period.

Then, use document.getElementById("wage-paid").innerHTML = <Current value here>;

in JavaScript to update the contents. For doing so, use a “live method,” that facilitates on-the-page communication between the browser and the oTree server, see

See https://otree.readthedocs.io/en/latest/live.html.

You can also search this forum for “live_method” to find examples.

Write a reply

Set forum username