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 onblur Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onblur
attribute is a fundamental feature in HTML that allows developers to define a JavaScript event handler to be executed when an element loses focus.
This attribute is particularly useful for validating user input, performing actions when users move away from a form field, or triggering specific behaviors when an element is no longer selected.
🎯 Purpose of onblur
The primary purpose of the onblur
attribute is to provide a way to execute JavaScript code when the user moves away from an input field or another interactive element.
This can be beneficial for enhancing user experience by providing instant feedback, validating input, or performing actions based on user interactions.
💎 Values
The onblur
attribute takes a JavaScript function as its value.
This function will be executed when the associated element loses focus. For example:
<input type="text" onblur="validateInput()">
🧠 How it Works
In this example, the validateInput function will be called when the user moves away from the text input field.
📄 Example
Let's consider a practical example where the onblur
attribute is used to validate an email address as the user leaves the input field:
<label for="email">Email:</label>
<input type="email" id="email" onblur="validateEmail()">
<script>
function validateEmail() {
var emailInput = document.getElementById("email");
var emailValue = emailInput.value;
// Perform email validation logic
// ...
// Provide user feedback or take appropriate actions
}
</script>
🧠 How it Works
In this example, the validateEmail function is triggered when the user moves away from the email input field, allowing you to perform validation checks and provide immediate feedback.
🔄 Dynamic Values with JavaScript
Similar to other HTML attributes, the onblur
attribute can be dynamically assigned using JavaScript. For instance:
<script>
// Dynamically assign onblur event handler
document.getElementById("dynamicField").onblur = function() {
// Your custom logic here
};
</script>
🧠 How it Works
This dynamic approach allows you to set the onblur behavior based on specific conditions or user interactions.
🏆 Best Practices
- Use the
onblur
attribute for tasks such as input validation, feedback, or triggering specific actions when a user moves away from a form field. - Keep the executed JavaScript code concise and focused on the intended purpose to maintain code readability.
- Test your implementation across different browsers to ensure consistent behavior.
🎉 Conclusion
The onblur
attribute is a powerful tool for handling user interactions in HTML forms.
By leveraging this attribute, you can create more interactive and user-friendly web applications, enhancing the overall experience for your website visitors.
👨💻 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 onblur Attribute), please comment here. I will help you immediately.