Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!