admin管理员组文章数量:1312655
Here, there is a button and two more check boxes are added. I am trying to do is, when I click on any check box, the Button color should change to green or any other color. If I unchecked all the boxes, the button color should change to gray color. Please help me. Thank you.
.input {
font-weight: bold;
border-radius: 1px;
background-color: grey;
}
<input id="input" class="" value=" ... " type="button">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
Here, there is a button and two more check boxes are added. I am trying to do is, when I click on any check box, the Button color should change to green or any other color. If I unchecked all the boxes, the button color should change to gray color. Please help me. Thank you.
.input {
font-weight: bold;
border-radius: 1px;
background-color: grey;
}
<input id="input" class="" value=" ... " type="button">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
Share
Improve this question
edited Aug 15, 2016 at 9:30
Ivan
40.8k8 gold badges73 silver badges117 bronze badges
asked Aug 15, 2016 at 9:19
Melbin MathaiMelbin Mathai
5071 gold badge8 silver badges26 bronze badges
5 Answers
Reset to default 4Use
document.querySelectorAll
to select elementsUse
.check-box:checked
to selectchecked
elements[:checked
]
var elems = document.querySelectorAll('.check-box');
var btn = document.querySelector('.btn-elem');
[].forEach.call(elems, function(el) {
el.addEventListener('change', function() {
var checked = document.querySelectorAll('.check-box:checked');
if (checked.length) {
btn.style.backgroundColor = 'green';
} else {
btn.style.backgroundColor = '';
}
});
});
<input class="btn-elem" value=" ... " style="font-weight: bold; border-radius: 1px; background-color: grey;" type="button">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
Here is an example using pure javascript only (no jquery) :
var button = document.getElementById("mybtn");
var chkbox = document.getElementsByClassName("chkbox");
function onChangeListener() {
button.style.backgroundColor = "gray";
for (var i = 0; i < chkbox.length; i++) {
if (chkbox[i].checked) {
button.style.backgroundColor = "green";
}
}
}
for (var i = 0; i < chkbox.length; i++) {
var checkbox = chkbox[i];
checkbox.addEventListener("change", onChangeListener);
}
<input type="button" id="mybtn" value=" . . . " style="background-color: gray;">
<input type="checkbox" class="chkbox">
<input type="checkbox" class="chkbox">
<input type="checkbox" class="chkbox">
Fiddle here : https://jsfiddle/avh4dsnm/1/
To elaborate slightly on Rayon's excellent answer and to make this a bit more dynamic. You could use a data attribute to specify the colour for the button to be for example
<input type="checkbox" class="check-box" data-color="green">
<input type="checkbox" class="check-box" data-color="blue">
You would then need another variable for the colour
var colour = checked.dataset.color;
if (checked.length) {
btn.style.backgroundColor = colour;
} else {
btn.style.backgroundColor = '';
}
Just for fun, here's a CSS-only solution:
#input {
font-weight: bold;
border-radius: 1px;
background-color: grey;
float:left;
}
input:checked ~ #input {
background:green;
}
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input type="checkbox" class="check-box">
<input id="input" class="" value=" ... " type="button">
what should be the code if I am using a multiple buttons?
checkbox 1 --> button 1
checkbox 2 --> button 2
var elems = document.querySelectorAll('.check-box');
var btn = document.querySelector('.btn-categ1');
[].forEach.call(elems, function(el) {
el.addEventListener('change', function() {
var checked = document.querySelectorAll('.check-box:checked');
if (checked.length) {
btn.style.backgroundColor = '#A6EBF2';
} else {
btn.style.backgroundColor = '';
}
});
});
本文标签: javascriptHow to change the Color of the button When click the CheckboxStack Overflow
版权声明:本文标题:javascript - How to change the Color of the button When click the Checkbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741882048a2402804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论