← Back to jQuery Course | Chapter 2: Selectors | Lesson 3 of 10

Form Selectors

The input selector targets input elements.

Input Selector

':input' matches every form control on the page, including textareas, selects, and buttons, not just <input> tags. It's the broadest of jQuery's form-related selectors.

Example: Input Selector

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input><textarea></textarea><button>Go</button>
    <script>
      console.log($(":input").length);
    </script>
  </body>
</html>

Text Input Selector

The ':text' selector narrows that down to just single-line text inputs, ignoring every other control type like checkboxes or buttons. It's shorthand for input[type="text"] but slightly slower since it can't use a native browser lookup.

Example: Text Input Selector

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input type="text"><input type="checkbox">
    <script>
      console.log($(":text").length);
    </script>
  </body>
</html>

Password Selector

The ':password' selector targets only password fields, which is useful when you need to validate or mask that specific input differently from regular text fields.

Example: Password Selector

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input type="password"><input type="text">
    <script>
      console.log($(":password").length);
    </script>
  </body>
</html>

Checkbox and Radio

':checkbox' and ':radio' let you select those specific control types directly, which is often paired with .prop(checked) to read or set whether they're currently selected.

Example: Checkbox and Radio

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input type="checkbox" checked><input type="radio">
    <script>
      console.log($(":checkbox").prop("checked"));
    </script>
  </body>
</html>

Submit and File

jQuery also provides ':submit' for submit buttons and ':file' for file-upload inputs, rounding out the full set of form-control selectors you can target individually.

Example: Submit and File

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input type="submit"><input type="file">
    <script>
      console.log($(":submit").length, $(":file").length);
    </script>
  </body>
</html>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.