jQuery :visible Selector

The jQuery :visible selector selects all the visible elements by not considering the below conditions..

  • The element which are set to a CSS property display:none.
  • Hidden fields inside a form using type="hidden".
  • The width and height of the element are zero(0).
  • Elements whose parents are hidden also hiding the child element as well.

You can use the selector at the time when you want to select of find only the visible elements. Visible elements are taking some space on the webpage.

Syntax of jQuery :visible Selector

See the syntax of jQuery :visible selector below.

Syntax
$(“:visible”)

The selector require no arguments to pass to use it. For the specific element to select, you have to use the element name, its class or id, at the start of the keyword visible.

Let’s check the examples given below to make it more clear.

How jQuery :visible Selector Select All Visible Elements

If you want to select only the visible elements of the list which is not set to the CSS property display:none. See the below example which color the text of only the visible list items. It uses the click event of jQuery to apply the effect on button click.

Example

Test it Live

Output

  • Table
  • Chair
  • Dining Table
  • Comfortable Chair

The above example contains the list of items and the button element. You have to just click the button to highlight the visible list items. It applies the ‘red’ color to the text of visible list items. There are two list items which are in hidden state and not taking any space in the example above.

Find Value of All Visible Elements in a Form

A form element can contains many hidden fields to get data from users. The form given below contains four input fields with two input field are using the type="hidden". We cannot say these two hidden input fields as visible.

The example checks all the input fields and displays the values in the alert box for the visible items.

Example

Test it Live

Output







To get the values of the visible input fields in the alert box, just click the button given above. It displays the alert box for two times as there are only two visible input fields in the form.