← Back to HTML Course | Chapter 5: Forms | Lesson 3 of 7

HTML Form Elements

एक फॉर्म को अपने TV के रिमोट कंट्रोल की तरह सोचिए। रिमोट की बॉडी खुद main form टैग है, जबकि उसके बटन, स्लाइडर्स, और डायल्स अलग-अलग form elements हैं। हर एलिमेंट का एक खास काम होता है: एक text बॉक्स यूज़र को टाइप करने देता है, एक dropdown उन्हें सिलेक्ट करने देता है, और एक बटन सिग्नल भेजता है। एक बेहतरीन फॉर्म बनाने के लिए, आपको हर टास्क के लिए सही एलिमेंट इस्तेमाल करना होगा ताकि यूज़र्स आपके पेज के साथ आसानी से इंटरैक्ट कर सकें। सही form elements इस्तेमाल करने से आपके forms screen readers के लिए accessible और नेविगेट करने में आसान बने रहते हैं।
Syntax
markup
<input type="type" name="name">
<select name="name">
  <option value="value">Label</option>
</select>
<textarea name="name" rows="number" cols="number"></textarea>
<button type="submit">Label</button>

बहुमुखी Input एलिमेंट

input टैग सबसे सामान्य form एलिमेंट है। इसके type attribute को बदलकर, यह अलग-अलग तरह का यूज़र डेटा इकट्ठा करने के लिए text fields, checkboxes, या file uploaders में बदल सकता है।

Note:
  • अपने inputs पर हमेशा एक name attribute तय करें, क्योंकि यही वह key है जिसका उपयोग डेटा को सर्वर पर भेजने में किया जाता है।
  • Accepted attributes:
  • type — text | password | email | number | date | checkbox | radio | file | submit | range | color ...
  • name — key used when submitting
  • value — initial value
  • placeholder — hint text
  • required / disabled / readonly — booleans
  • autocomplete — on | off
Warning: input एक self-closing टैग है; closing input टैग लिखने की कोशिश करना invalid HTML है।

उदाहरण: The Versatile Input Element

markup
<input type="text" name="username">
<input type="checkbox" name="agree">
<input type="file" name="upload">

Label एलिमेंट

label एलिमेंट एक input फील्ड के लिए स्पष्ट टेक्स्ट लेबल देता है। अपने labels को inputs से लिंक करना वेब accessibility के लिए ज़रूरी है और छोटे inputs (जैसे checkboxes) को क्लिक करना काफी आसान बना देता है।

Note:
  • किसी label को input से लिंक करने के लिए label के for attribute को input के id attribute से मैच कराएं।
  • Accepted attributes:
  • for — id of the associated form control
  • Alternative: nest the control inside the label
Warning: अपने labels को लिंक न करने से visually impaired यूज़र्स के लिए आपके forms नेविगेट करना मुश्किल हो जाता है।

उदाहरण: The Label Element

markup
<label for="agree">I agree to the terms</label>
<input type="checkbox" id="agree">

select और option के साथ Dropdown Selections

select एलिमेंट एक साफ-सुथरा, कॉम्पैक्ट dropdown मेनू बनाता है। यह कई option टैग्स को एक साथ ग्रुप करता है, जिससे यूज़र अपने पेज लेआउट को गन्दा किए बिना किसी लिस्ट से एक वैल्यू चुन सकते हैं।

Note:
  • यूज़र को यह गाइड करने के लिए कि उन्हें क्या चुनना है, 'disabled selected' के साथ एक placeholder ऑप्शन जोड़ें।
  • Accepted attributes:
  • select multiple — boolean
  • select size — number of visible rows
  • select name / required / disabled
  • option value — submitted value
  • option selected — boolean
  • option disabled — boolean
  • optgroup label — group name
Warning: अपने option टैग्स पर हमेशा value attribute शामिल करें, क्योंकि यही वह असली डेटा है जो आपके सर्वर पर भेजा जाता है।

उदाहरण: Dropdown Selections with select and option

markup
<select name="size">
  <option value="s">Small</option>
  <option value="m">Medium</option>
</select>

textarea के साथ Multi-Line Text

जब आपको एक लाइन से ज़्यादा टेक्स्ट चाहिए (जैसे यूज़र फीडबैक या विश्लेषण के लिए पैराग्राफ), तो textarea एलिमेंट आपका सबसे अच्छा विकल्प है। standard inputs के उलट, यह टेक्स्ट की कई लाइनें रख सकता है और डिफ़ॉल्ट रूप से resizable होता है।

Note:
  • अपने textarea के शुरुआती डिस्प्ले साइज़ को तय करने के लिए rows और cols attributes का उपयोग करें।
  • Accepted attributes:
  • rows — visible lines (integer)
  • cols — visible character width (integer)
  • name / placeholder / required / readonly / disabled
  • maxlength / minlength — integer
  • wrap — soft | hard | off
Warning: textarea टैग self-closing नहीं है; आपको closing टैग लिखना ही होगा, भले ही आप इसे खाली शुरू करना चाहें।

उदाहरण: Multi-Line Text with textarea

markup
<textarea name="feedback" rows="4" cols="30"></textarea>

button एलिमेंट

button टैग का उपयोग फॉर्म डेटा सबमिट करने या आपके पेज पर एक्शन ट्रिगर करने के लिए किया जाता है। इसके type attribute को submit सेट करने पर, यह ब्राउज़र को निर्देश देता है कि वह सारे फॉर्म डेटा को पैकेज करके आपके सर्वर पर भेज दे।

Note:
  • डेटा सबमिट करने की बजाय कस्टम JavaScript एक्शन ट्रिगर करने वाले बटन के लिए type="button" का उपयोग करें।
  • Accepted attributes:
  • type — submit | reset | button
  • name / value
  • disabled — boolean
  • form — id of associated form
Warning: अगर आप form के अंदर type attribute स्पष्ट नहीं करते, तो ब्राउज़र्स इसे डिफ़ॉल्ट रूप से submit बटन मान लेते हैं, जिससे अनचाहे form submissions हो सकते हैं।

उदाहरण: The button Element

markup
<button type="submit">Send</button>
<button type="button">Run Script</button>
Live Example
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. #}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.