jQuery Topics
- jQuery Introduction
- jQuery Callbacks
- jQuery deferred
- jQuery selectors
- *
- :animated
- [name|=”value”]
- [name*=”value”]
- [name~=”value”]
- [name$=”value”]
- [name=”value”]
- [name!=”value”]
- [name^=”value”]
- :button
- :checkbox
- :checked
- Child Selector
- .class
- :contains()
- Descendant Selector
- :disabled
- Element
- :empty
- :enabled
- :eq()
- :even
- :file
- :first-child
- :first-of-type
- :first
- :focus
- :gt()
- Has Attribute
- :has()
- :header
- :hidden
- #id
- :image
- :input
- :lang()
- :last-child
- :last-of-type
- :last
- :lt()
- [name=”value”][name2=”value2″]
- (“selector1, selector2, selectorN”)
- (“prev + next”)
- (“prev ~ siblings”)
- :not()
- :nth-child()
- :nth-last-child()
- :nth-last-of-type()
- :nth-of-type()
- :odd
- :only-child
- :only-of-type
- :parent
- :password
- :radio
- :reset
- :root
- :selected
- :submit
- :target
- :text
- :visible
- jQuery Ajax Events
- jQuery Ajax Methods
- jQuery Keyboard Events
- jQuery Keyboard Methods
- jQuery Form Events
- jQuery Form Methods
- jQuery Mouse Events
- jQuery Mouse Methods
- jQuery Event Properties
- jQuery Event Methods
- jQuery HTML
- jQuery CSS
- jQuery Fading
- jQuery Traversing
- jQuery Utilities
- jQuery Properties
jQuery :checked Selector
Photo Credit to CodeToFun
🙋 Introduction
jQuery simplifies web development by providing powerful tools for manipulating HTML elements and handling events. One such tool is the :checked
selector, which allows you to target checked checkboxes and radio buttons effortlessly. Mastering this selector can greatly enhance your ability to create interactive and dynamic web pages.
In this guide, we'll delve into the usage of the jQuery :checked
selector with clear examples to help you understand its potential.
🧠 Understanding :checked Selector
The :checked
selector is designed to target HTML elements that are checked, primarily checkboxes and radio buttons. It is especially useful when you want to perform actions based on the checked state of these inputs.
💡 Syntax
The syntax for the :checked
selector is straightforward:
$(":checked")
📝 Example
Selecting Checked Checkboxes:
Suppose you have a group of checkboxes and you want to select all the checked ones. You can achieve this using the
:checked
selector as follows:index.htmlCopied<input type="checkbox" id="checkbox1" checked> <input type="checkbox" id="checkbox2"> <input type="checkbox" id="checkbox3" checked>
example.jsCopied$("input:checked").each(function() { console.log($(this).attr('id')); });
This will log the IDs of the checked checkboxes (checkbox1 and checkbox3) to the console.
Styling Checked Elements:
You can apply CSS styles to checked elements dynamically using jQuery. For instance, let's change the background color of checked checkboxes:
index.htmlCopied<input type="checkbox" id="checkbox1" checked> <input type="checkbox" id="checkbox2"> <input type="checkbox" id="checkbox3" checked>
example.jsCopied$("input:checked").css("background-color", "lightblue");
This will set the background color of checked checkboxes to light blue.
Handling Events on Checked Elements:
You can also bind events to checked elements using jQuery. Here's an example where we alert a message when a checkbox is checked:
index.htmlCopied<input type="checkbox" id="checkbox1">
example.jsCopied$("#checkbox1").change(function() { if($(this).is(":checked")) { alert("Checkbox is checked!"); } });
Unchecking Checkboxes Programmatically:
If you need to uncheck checkboxes based on certain conditions, jQuery can simplify the task. For example:
index.htmlCopied$("#uncheckButton").click(function() { $("input:checked").prop("checked", false); });
This will uncheck all checked checkboxes when a button with the ID uncheckButton is clicked.
🎉 Conclusion
The jQuery :checked
selector is a powerful tool for selecting and manipulating checked checkboxes and radio buttons. Whether you want to perform actions, style elements, handle events, or even manipulate the checked state programmatically, this selector provides an efficient solution.
By mastering its usage, you can create more interactive and dynamic web pages effortlessly.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (jQuery :checked Selector), please comment here. I will help you immediately.