A
A
Alisa942019-03-14 13:40:11
JavaScript
Alisa94, 2019-03-14 13:40:11

How to apply one onclick function to multiple buttons?

I have a list of buttons in a sidebar and two input fields (number one and number two) in the main container. I need to make it so that when you click on the first (or second, third, etc.) button, the name of this button is sent to the main container in the number one input field. Then, when you clicked on the second (or first, or third, etc.) button, the name of this button was sent to the main container in the input field number two. I think I should do it with the onclick event, but the implementation method doesn't really matter to me. Below is my code, which sends the name of the pressed button only to the first input field (field number one).
JS

function projectClick(index, name) {
  const nameA = document.getElementById('prjA');
  const nameB = document.getElementById('prjB');   
  nameA.value = name;
}

pug
mixin projectButton(objectName index) 
  lable
    input.prjBtn(type="button" value=`${objectKey}` onclick="projectClick('" + index + "','" + objectKey + "')")

block append main
  form.formMod
    input.form-control#prjA(type="text" name="prjA")
    input.form-control#prjB(type="text" name="prjB")

block append sidebar
  form.formMod
    each val, index in projectlist
      +projectButton(val.objectKey, index)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question