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

Form Selectors

input selector input elements को target करता है।
Syntax
javascript
$(":input")
$(":text")
$(":checkbox")
$(":radio")
$(":submit")

Input Selector

':input' page पर हर form control को match करता है, सिर्फ <input> tags नहीं बल्कि textareas, selects, और buttons भी शामिल करते हुए। यह jQuery के form-related selectors में सबसे broad है।

उदाहरण: 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

':text' selector इसे सिर्फ single-line text inputs तक narrow कर देता है, checkboxes या buttons जैसी हर दूसरी control type को नज़रअंदाज़ करते हुए। यह input[type="text"] का shorthand है लेकिन थोड़ा धीमा है क्योंकि यह एक native browser lookup इस्तेमाल नहीं कर सकता।

उदाहरण: 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

':password' selector सिर्फ password fields को target करता है, जो तब उपयोगी है जब आपको उस specific input को regular text fields से अलग तरीके से validate या mask करना हो।

उदाहरण: 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 और Radio

':checkbox' और ':radio' आपको उन specific control types को सीधे select करने देते हैं, जो अक्सर यह पढ़ने या set करने के लिए .prop(checked) के साथ जोड़े जाते हैं कि वे इस समय selected हैं या नहीं।

उदाहरण: 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 और File

jQuery submit buttons के लिए ':submit' और file-upload inputs के लिए ':file' भी provide करता है, form-control selectors के पूरे set को complete करते हुए जिन्हें आप individually target कर सकते हैं।

उदाहरण: 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>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि :input सिर्फ <input> tags match करता है; यह <textarea>, <select> और <button> को भी match करता है।
  2. :text इस्तेमाल करना और उम्मीद करना कि यह किसी <textarea> या बिना type attribute वाले input को reliably ढूँढ लेगा, जबकि यह सिर्फ input[type=text] को target करता है।
  3. $(":checkbox") लिखना और सिर्फ checked boxes की उम्मीद करना; यह हर checkbox select करता है, और narrow करने के लिए आपको :checked चाहिए।

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.