javascript - Jquery Change background color of DIV to #333 only when all checkbox is not checked - Stack Overflow
I have 5 div
with class .thumb-folder
and inside each contains a checkbox
.
I also have another div
with a class .alarm
.
When 1 or many checkboxes
is checked the div
with .alarm class
changes background to red.
How to change the div
with the .alarm class
to background #333
only when no checkboxes
are checked?
Here is a link to jsfiddle of my current code.
I have 5 div
with class .thumb-folder
and inside each contains a checkbox
.
I also have another div
with a class .alarm
.
When 1 or many checkboxes
is checked the div
with .alarm class
changes background to red.
How to change the div
with the .alarm class
to background #333
only when no checkboxes
are checked?
Here is a link to jsfiddle of my current code.
Share Improve this question edited Jan 18, 2013 at 4:38 Mithun Satheesh 27.9k14 gold badges79 silver badges102 bronze badges asked Jan 18, 2013 at 4:28 guitarPHguitarPH 2012 silver badges16 bronze badges3 Answers
Reset to default 5Use the sam eselector you used to bind the handler to check if any checkboxes are checked.
$('.thumb-folder input:checkbox').on('click', function (e) {
if ($('.thumb-folder input:checkbox').is(':checked')) {
$('div.alarm').css("background-color","red");
}
else{
$('div.alarm').css("background-color","#333");
}
});
DEMO
You can use length to find the checked checkbox count using :checked
Live Demo
$('.thumb-folder input:checkbox').on('click', function (e) {
if ($('.thumb-folder :checked').length == 0)
$('div.alarm').css("background-color", "#333");
else
$('div.alarm').css("background-color", "red");
});
You can just do this in 3 lines:
$('.thumb-folder input:checkbox').on('click', function (e) {
$('div.alarm').css("background-color",(($("input[type='checkbox']:checked").length > 0) ? "red" : "#333"));
});
- 人工智能的温度:AI究竟会带给我们怎样的世界?
- open source - Langgraph State not being passed properly with Ollama - Stack Overflow
- reactjs - When i run the build command on my project, the dist files aren't being created in the correct format - Stack
- Vercel hosted SvelteKit app keeps 404ing on form submit? - Stack Overflow
- python - How to sub-class LangGraph's MessageState or use Pydantic for channel separation - Stack Overflow
- solrj - How to enable using a magic field with eDisMax in Solr 9.4? - Stack Overflow
- flutter - Error handshake exception: handshake error in client(os error: certificate _verify_failed: unable to get local issuer
- sql - PostgreSQL ERROR: cannot accumulate arrays of different dimensionality - Stack Overflow
- vue.js - single pages doesn't work when i moved them to app component - Stack Overflow
- ggplot2 - alluvial diagram in R, Error: Data not in recognizable format - Stack Overflow
- java - Bluej throws SSLHandshakeException making http request - Stack Overflow
- linker - Appending to an ELF file - Cortex MGCC - Stack Overflow
- python - How can I use FilteredSelectMultiple widget in a django custom form? - Stack Overflow
- python - Can't run pytest with a `FileNotFoundError: [Errno 2] No such file or directory: 'tmppip-build-env-3fag
- Odoo: BOM Update Not Removing Deleted Items from RFQ in Purchase Orders - Stack Overflow
- swift - Infinite Update Loop with .onGeometryChange and Padding - Stack Overflow
- OneNote with embeded Excel having Data Connection - security warning and disabled - Stack Overflow