1

I am trying to save the timestamp from a click in a database on Otree. I tried using differents codes.

The first one is this

function myFunction() {
    var n = Date.now();
document.getElementById("demo").innerHTML = n;
}

document.getElementById('n').value;

I put the next code in the html:

 <p style="text-align: center;"><button class="btn btn-primary btn-large" onclick="myFunction()"  name="offer_accepted" value="True">&nbsp;A</button> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary btn-large"  onclick="myFunction()" name="offer_accepted" value="False">B</button></p>

The next one is this:

function record_current_time(){
   var current_time = new Date();
   current_time = current_time.getTime()
   document.getElementById("id_currentTime").setAttribute("value", current_time)
}

window.onload = record_current_time;

And put the next code in my html:

  <input type="hidden" name="currentTime" id="id_currentTime"/>

In both cases I don't get anything in my database.

Can someone help me

1
  • Have you defined the corresponding field in your model? Have you tried generating the hidden input via the page template (i.e. {% formfield player.currentTime %}) rather than hard-coding it? How is the form submitted? Commented Nov 18, 2019 at 12:50

1 Answer 1

1

Your first code doesn't work for two reasons:

  • You are using an ID that doesn't exist: "demo"
  • If you substitute .innerHTML with .value your code will work. See the discussion here.

Regarding the second try, this command will work:

window.onload = function() {
    record_current_time();
}

Another possibility that works with the script at the top of the page is:

$(document).ready(function(){
        function record_current_time(){
           var current_time = new Date();
           current_time = current_time.getTime();
           document.getElementById('id_currentTime').value = current_time;
        }

        record_current_time();
});

However, you could also just put the script to the bottom of the page. Then all elements will be loaded when you execute the scripts.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.