0

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?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I made you a snippet. I changed the hidden field to text to show – mplungjan Nov 06 '19 at 15:40
  • You want to store the timestamp in the hidden field timestamp and the button ID in c1? – mplungjan Nov 06 '19 at 15:40
  • Hi, the button ID is in the button html. So when I click the A bottom I get a 0 in my database and when I click the B bottom I get a 1. I want to submit each button using different keypresses. For example if I press the letter S I submit the button A and when i press the letter K I submit the bottom B. – Sebastián Ramírez Nov 06 '19 at 15:47
  • "submit" where is your form? You can add formAction to the button but if you have a form, the javascript I posted will set the values to be sent – mplungjan Nov 06 '19 at 16:05
  • Hi, actually the first code sent the values. The problem is that the buttons come from a Boolean form so I only have one form. I need to submit the 1 buttom with a keypress and the 0 button with a different one. You gave a code that submit the code with clicks and no with keypresses – Sebastián Ramírez Nov 10 '19 at 21:15
  • I do not understand. You have onclick on both buttons. Please describe the actions you expect – mplungjan Nov 11 '19 at 05:07

1 Answers1

0

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 }}
mplungjan
  • 169,008
  • 28
  • 173
  • 236