Detailed explanation of HTML onfocus gain focus and onblur lose focus events

Detailed explanation of HTML onfocus gain focus and onblur lose focus events

HTML onfocus Event Attributes

Definition and Usage

The onfocus attribute is triggered when an element receives focus.

onfocus is commonly used for <input>, <select> and <a>.

Tip: The onfocus attribute is the opposite of the onblur attribute.

Note: The onfocus attribute does not apply to the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.

Examples

Triggers a function when the input field gets focus. This function changes the background color of the input field

<script>
function setStyle(x)
{
document.getElementById(x).style.background="yellow";
}
</script>
</head>
<body>
 
<p>Function triggered when the input field gets focus. This function changes the background color of the input field. </p>
 
First name: <input type="text" id="fname" onfocus="setStyle(this.id)"><br>
Last name: <input type="text" id="lname" onfocus="setStyle(this.id)">
 
</body>

HTML onblur Event Attributes

Definition and Usage

The onblur attribute is triggered when the element loses focus.

onblur is commonly used in form validation code (such as when a user leaves a form field).

Examples

Validate the input field when the user leaves it:

<script>
function upperCase()
{
var x = document.getElementById("fname").value
document.getElementById("fname").value=x.toUpperCase()
}
</script>
</head>
<body>
 
<p>Please enter your name and move focus outside the field:</p>
Please enter your name (in English characters): <input type="text" name="fname" id="fname" onblur="upperCase()">
 
</body> 

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Vendor Prefix: Why do we need a browser engine prefix?

>>:  mysql splits a row of data into multiple rows based on commas

Recommend

Summary of Vue component basics

Component Basics 1 Component Reuse Components are...

Install mysql5.7.13 using RPM in CentOS 7

0. Environment Operating system for this article:...

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

Detailed explanation of Redis master-slave replication practice using Docker

Table of contents 1. Background 2. Operation step...

How to enter and exit the Docker container

1 Start the Docker service First you need to know...

Analysis of the Poor Performance Caused by Large Offset of LIMIT in MySQL Query

Preface We all know that MySQL query uses the sel...

Vue project code splitting solution

Table of contents background Purpose Before split...

Solution to Tomcat server failing to open tomcat7w.exe

I encountered a little problem when configuring t...

MySQL 5.7.11 zip installation and configuration method graphic tutorial

1. Download the MySQL 5.7.11 zip installation pac...

IIS and APACHE implement HTTP redirection to HTTPS

IIS7 Download the HTTP Rewrite module from Micros...

Installation and configuration tutorial of MySQL 8.0.16 under Win10

1. Unzip MySQL 8.0.16 The dada folder and my.ini ...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

How to bind domain name to nginx service

Configure multiple servers in nginx.conf: When pr...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...