HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- 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 Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML formtarget Attribute
Photo Credit to CodeToFun
🙋 Introduction
The formtarget
attribute is a useful feature in HTML that allows developers to specify where to display the response that is received after submitting a form. This attribute can be applied to the <form> element and individual form controls, such as buttons and input fields.
By utilizing the formtarget
attribute, developers can control the behavior of form submissions and decide whether the response should be displayed in the same window, a new window, or a specific frame.
🎯 Purpose of formtarget Attribute
The primary purpose of the formtarget
attribute is to define the target window or frame where the response to a form submission should be displayed. By default, when a form is submitted, the response is displayed in the same window/tab. However, there are scenarios where developers may want to change this behavior, such as displaying the response in a new browser window or a specific iframe within the page.
💎 Values
The formtarget
attribute accepts various values to determine the target location for displaying the form response. Some of the common values include:
- _self: This is the default value. It specifies that the response should be displayed in the same window/tab that the form was submitted from.
- _blank: This value opens the response in a new browser window/tab.
- _parent: This value targets the parent window or frame of the current window.
- _top: This value targets the top-level browsing context (the entire window).
- framename: If the value is the name of a specific frame or iframe, the response is displayed in that frame.
📄 Implementation Example:
Let's illustrate the usage of the formtarget
attribute with an example:
<form action="submit.php" method="post" target="_blank">
<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="Submit" formtarget="_parent">
</form>
🧠 How it Works
In this example, the form has its target attribute set to "_blank", which means the response will open in a new browser window/tab by default. However, the submit button has its formtarget
attribute set to "_parent", instructing the browser to display the response in the parent window instead.
🔄 Dynamic Values with JavaScript
Similar to other HTML attributes, the formtarget
attribute can also be set dynamically using JavaScript. This allows developers to change the target location based on user interactions or certain conditions. Here's a basic example:
<script>
// Dynamically set the formtarget attribute
document.getElementById("myForm").setAttribute("formtarget", "_top");
</script>
🧠 How it Works
In this script, the formtarget
attribute of a form with the id "myForm" is dynamically set to "_top", instructing the browser to display the form response in the top-level browsing context.
🏆 Best Practices
- Consider the user experience and usability when choosing the target for form submissions. Opening responses in new windows or frames should be done judiciously to avoid confusing users.
- Ensure compatibility with different browsers and devices, as the behavior of formtarget may vary.
- Test your forms thoroughly to ensure that the desired target behavior is achieved and that the user experience is seamless.
🎉 Conclusion
The formtarget
attribute provides developers with flexibility and control over where form responses are displayed.
By understanding how to utilize this attribute effectively, you can enhance the functionality and usability of your web forms.
👨💻 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 formtarget Attribute), please comment here. I will help you immediately.