jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
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.