Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

jQuery :checked Selector

Posted in jQuery Tutorial
Updated on Apr 10, 2024
By Mari Selvan
👁️ 35 - Views
⏳ 4 mins
💬 0
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:

syntax.js
Copied
Copy To Clipboard
$(":checked")

📝 Example

  1. 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.html
    Copied
    Copy To Clipboard
    <input type="checkbox" id="checkbox1" checked>
    <input type="checkbox" id="checkbox2">
    <input type="checkbox" id="checkbox3" checked>
    example.js
    Copied
    Copy To Clipboard
    $("input:checked").each(function() {
        console.log($(this).attr('id'));
    });

    This will log the IDs of the checked checkboxes (checkbox1 and checkbox3) to the console.

  2. 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.html
    Copied
    Copy To Clipboard
    <input type="checkbox" id="checkbox1" checked>
    <input type="checkbox" id="checkbox2">
    <input type="checkbox" id="checkbox3" checked>
    example.js
    Copied
    Copy To Clipboard
    $("input:checked").css("background-color", "lightblue");

    This will set the background color of checked checkboxes to light blue.

  3. 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.html
    Copied
    Copy To Clipboard
    <input type="checkbox" id="checkbox1">
    example.js
    Copied
    Copy To Clipboard
    $("#checkbox1").change(function() {
        if($(this).is(":checked")) {
            alert("Checkbox is checked!");
        }
    });
  4. Unchecking Checkboxes Programmatically:

    If you need to uncheck checkboxes based on certain conditions, jQuery can simplify the task. For example:

    index.html
    Copied
    Copy To Clipboard
    $("#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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy