Search code examples
javascripthtmlotree

Submit two button with different keycodes


I have the following code to save a person's timestamps when he clicks on one of two buttons.

On the screen they must choose which button they prefer if A or B.

function myFunction() {
  var n = Date.now();
  document.getElementById("c1").value = n;
}
<p style="text-align: center;">
  <button class="btn btn-primary btn-large" 
    onclick="myFunction()" name="offer_accepted1" value="True">&nbsp;A</button> 
  <button class="btn btn-primary btn-large"
    onclick="myFunction()" name="offer_accepted1" value="False">B</button>
</p>
<input type="hidden" name="timestamp1" value="0" />
{{ form.timestamp1.errors }}

<input type="text" id="c1" name="c1" value="0" />
{{ form.c1.errors }}

I want to program two keys to submit the buttons. But I need each key to be one of the buttons to know which option they chose. Also, I don't want to lose the timestamp. Can someone help me?


Solution

  • You mean this?

    I added ID to field

    function myFunction(but) {
      var n = Date.now();
      document.getElementById("timestamp1").value = n;
      document.getElementById("c1").value = but.value;
    }
    <p style="text-align: center;">
      <button class="btn btn-primary btn-large" 
        onclick="myFunction(this)" name="offer_accepted1" value="True">&nbsp;A</button> 
      <button class="btn btn-primary btn-large"
        onclick="myFunction(this)" name="offer_accepted1" value="False">B</button>
    </p>
    <input type="text" name="timestamp1" id="timestamp1" value="0" />
    {{ form.timestamp1.errors }}
    
    <input type="text" id="c1" name="c1" value="0" />
    {{ form.c1.errors }}