Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass second button on click over the first button to a javascript function

I am new to javascript and html . I have a small question. I have some thing like the following in the javascript file.

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 function argument when click the first button instead of 'this' i use b2 ,but it not worked can anyone help please

like image 214
devendrak353 Avatar asked Sep 03 '26 01:09

devendrak353


1 Answers

You could use the id instead.

set the id to 'b2' and then inside the function find it and set it. You no longer have to use this anymore .. whatever id 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"/>

You can now pass any of them to the function

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

if (btn.style.backgroundColor == "#f47121") {
    btn.style.backgroundColor = color;
} else {
    btn.style.backgroundColor = "#f47121";
}
}
like image 144
scartag Avatar answered Sep 05 '26 15:09

scartag