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
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";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With