HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML form Attribute
Photo Credit to CodeToFun
🙋 Introduction
The form
attribute is a fundamental aspect of HTML that plays a crucial role in associating form controls with a specific form.
By utilizing the form
attribute, you can create more flexible and structured web forms, allowing for better organization and improved accessibility.
🎯 Purpose of form
The primary purpose of the form
attribute is to establish a relationship between form controls and a form element.
This association enables you to structure your HTML documents in a way that makes it clear which form an input element belongs to, even if they are not directly nested within the form element.
💎 Values
The form
attribute accepts the id of the form element it should be associated with. This allows you to reference a specific form elsewhere in the HTML document.
📄 Example
Let's look at a simple example of how to use the form
attribute in HTML:
<form id="loginForm" action="/submit-login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="submit" value="Login">
</form>
<!-- Associate a button with the form using the form attribute -->
<button form="loginForm" type="submit">Submit with Button</button>
🧠 How it Works
In this example, the form has an id of loginForm. The submit button outside the form is associated with the form using the form
attribute, creating a connection between the button and the form.
🔄 Dynamic Values with JavaScript
You can also dynamically set the form
attribute using JavaScript.
This can be useful when you want to change the association of form controls based on user interactions or other dynamic conditions. Here's an example:
<script>
// Dynamically set form attribute for an input field
document.getElementById("dynamicField").form = "registrationForm";
</script>
🧠 How it Works
In this script, the form
attribute is dynamically set to "registrationForm" for an input field with the id "dynamicField." This dynamic association can be beneficial when you need to update the form connection based on user actions.
🏆 Best Practices
- Use the
form
attribute to create clear and organized HTML documents, especially when dealing with multiple forms on a page. - Ensure that the id specified in the
form
attribute corresponds to the actual id of the form element you want to associate with. - Leverage the
form
attribute to connect form controls that are not directly nested within the form element, promoting a more modular and maintainable code structure.
🎉 Conclusion
The form
attribute is a crucial tool for establishing connections between form controls and form elements in HTML.
By incorporating this attribute into your web forms, you can enhance the structure and accessibility of your HTML documents.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML form Attribute), please comment here. I will help you immediately.