How to pass the second button when clicking the first button on a javascript function

I am new to javascript and html. I have a small question. The javascript file has something like the following.

function setColor(btn, color)
{
if (btn.style.backgroundColor == "#f47121") {
    btn.style.backgroundColor = color;
} else {
    btn.style.backgroundColor = "#f47121";
}
}

<input type="submit" value="Review & Next" name="b1" onclick="setColor(this,'#fff200');" />
<input type="submit" value="1" name="b2" />

      

My question is how to pass the second button as a function argument when the first button is clicked instead of 'this' I am using b2, but that didn't work. Can anyone help

+3


source to share


1 answer


You can use an ID instead.

set id to 'b2' and then inside the function find it and set it. You don't have to use it this

anymore ... any identifier you pass in the function will use that instead.

<input type="submit" value="Review & Next" name="b1" id="b1" onclick="setColor('b2','#fff200');" />
<input type="submit" value="1" name="b2" id="b2"/>

      



Now you can pass any of them to a function

function setColor(arg, color)
{
   var btn = document.getElementById(arg);

if (btn.style.backgroundColor == "#f47121") {
    btn.style.backgroundColor = color;
} else {
    btn.style.backgroundColor = "#f47121";
}
}

      

+2


source







All Articles