jQuery :checked Selector

The jQuery :checked selector selects the checkboxes and the radio button options if checked. Both are the elements come inside the form. When you want to highlight the checked items, you can use this selector.

After selecting the elements, you can apply CSS and other effects to the elements. However, checkboxes and radio buttons are the elements to which you cannot apply CSS directly. You have to wrap them with other elements to highlight. See the example given below to learn how to perform this task.

jQuery :checked Selector

Syntax of the Selector

The syntax of jQuery :checked selector is given below.

Syntax
$(“:checked”)

The selector requires no parameters to pass as the argument. However, you can use the class name, id, or the tag name of the element to select the specified element.

jQuery :checked Selector Selects Input Type Checkbox in a Form

To make the checkbox option checked, you have to add an attribute checked to the options. After that, you can find and select the checked elements using this selector. You can highlight the checked items by wrapping them inside the other elements. See the example given below.

Example

Test it Live

Output



Select Gender
Male
Female

The above example wrapped the checked items with a span element. After that, it applies the CSS to the span element and it highlights the checked item. The red-colored item shows that the option is checked.

The Selector Select Radio Button Options if Checked

In addition to the above checkbox, you can also select the radio box options if checked. To highlight the checked items, you also need to wrap the other elements like span elements. Apply the CSS to highlight the checked items as given in the below example.

Example

Test it Live

Output



Select Gender
Male
Female

The above example showing the highlighted radio options. You can find it as the green bordered items given above. It’s the best way to highlight only the required items to identify it easily.

If you have any queries regarding this tutorial post, please comment below.

Also tell me, what other methods you are using with the selector by commenting below.

References