Dear all,
I am trying to add a simple calculator to the pages where subjects make an investment decision (for trial I just used a public goods app from templates) . The calculator works for two numbers and when I clich "calculate" the result appears. For now I left the calculation process as summation for simplicity. I also added a function to the "Calculate" button to count how many times one used it.
Now I want to store the amount of clicks made by subjects in each page. I tried to research it in previous questions but I have very limited experience in js. Please find my code below :
1. Models:
class Player(BasePlayer):
num_clicks = models.IntegerField()
Template:
{% formfield 'contribution' %}
<script>
//calculator functions
function bank()
{
let x = document.getElementById("val1").value;
let y = document.getElementById("val2").value;
return +x + +y;
}
function solve()
{
let x = document.getElementById("val1").value;
let y = document.getElementById("val2").value;
document.getElementById("result").value = bank(x,y);
}
//counting the amount of clicks
var clicks = 0;
function track() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
}
</script>
<!-- values to be entered -->
First number: <input type="number" id = "val1" />
<br>
Second number: <input type="number" id = "val2" />
<br>
<button type="button" onclick="track();solve()">Calculate</button>
<br>
Result: <input type="text" id="result"/>
<br>
<!-- shows the amount of clicks to check if the function works. Will not be seen by subjects in the experiment -->
Clicks: <a id="clicks">0</a>
<input type="hidden" id="num_clicks" name="num_clicks" />
{% next_button %}
Thanks in advance for your helps,
Fatih