Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Passing argument to same button and checking the argument in function with if condition

I have 2 buttons that use the same function, I need to be able to identify which button I pressed.

On button 1 I put onclick="myFunction(1)" and on button 2 I put myFunction(2) then on myFunction(a) if I use if condition a=1 it won't execute.

<!DOCTYPE html>
<html>
<body>
    
<!-- CSR -->
<label onclick="myFunction(a.value=1)">CSR</label>
<input type="text" value="Hello World" id="CSRNm"><br>
    
<!-- Acct# -->
<label onclick="myFunction(a.value=2)">Acct#</label>
<input type="text" value="Hello Other World" id="AccNo">
    
<script>
function myFunction(a) {  
  if (a.value=1){
    navigator.clipboard.writeText(CSRNm.value);
    /* Alert the copied text */
    alert("Copied the text: " + a.value);
  } else if (a.value=2){
    navigator.clipboard.writeText(AccNo.value);
    /* Alert the copied text */
    alert("Copied the text: " + AccNo.value);
  }  
}
</script>
    
</body>
</html>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Why are you passing a.value just pass it as a as follows:

<button id="btn1" onclick="myFunction(a=1)">one</button>
<button id="btn2" onclick="myFunction(a=2)">two</button>
const myFunction = (a) => {
    if (a === 1) {
        console.log("a");
    }
    if (a === 2) {
        console.log("b");
    }
};

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of:

myFunction(a.value=1)

you can use this:

myFunction({value: 1})

The function declaration for myFunction already refers to the parameter as a.

So, when you pass the object literal {value: 1} as the function parameter, the executing function will return 1 when asked for a.value.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda