Detailed explanation of JavaScript onblur and onfocus events

Detailed explanation of JavaScript onblur and onfocus events

In HTML pages, visual elements such as buttons and text boxes have events for having and losing focus, which can trigger preset operations in response to mouse or keyboard actions. This article uses the example of a text box gaining and losing focus to briefly explain the application of onfocus and onblur.

1. onfocus (get focus event)

When a text box gets the focus, all the text in it will be automatically selected just like the Baidu search input box on the "hao123" website. This operation can be achieved using onfocus.

When you move the mouse pointer over the following text box, all the text inside is selected:

Please enter the URL

How is this done? See the following code and explanation:

<input type="text" name="url" value="http://www.gxblk.com" size="30" usemean="this.focus();this.select();">

In the code, the input tag embeds a JS statement for the onmousemove (mouse pointer passes over) event. The this.focus() after the equal sign means that it gets the focus. The sign of getting the focus is that the input cursor will appear in the text box, but to select all the text in it, we have to use the this.select() statement, which means selecting all the text in the text box.

2. onblur (loss of focus event)

We often check whether the text box has been correctly entered. The detection is usually performed after the user clicks the submit button. In fact, when the control loses focus, we can perform this detection in real time. In this case, the onblur event comes in handy.

The following example has four text boxes. If you haven't clicked any of them, nothing will happen. However, when you click any of them to give it focus (the input cursor is in it), if nothing is entered and you click somewhere else to make it lose focus, a warning will pop up. Try it.

Name

gender

age

address

Here is the code and explanation:

Form Code

<form name="blur_test">

   <p>Name<input type="text" name="name"value="" size="30"οnblur="chkvalue(this)"><br>

    Gender<inputtype="text" name="sex" value=""size="30" οnblur="chkvalue(this)"><br>

    Age<inputtype="text" name="age" value=""size="30" οnblur="chkvalue(this)"><br>

    Address<inputtype="text" name="addr" value=""size="30" οnblur="chkvalue(this)"></p>

</form>

JS code

<scriptlanguage="javascript">
function chkvalue(txt) {
   if(txt.value=="") alert("The text box must be filled in!");

}
</script>

In the form code, each box code is embedded with an onblur JS statement, which calls the custom function chkvalue(this) in the following JS code, which means that when the text box loses focus, the chkvalue() function is called; this chkvalue() function detects whether the text box is empty, and if so, a warning window pops up. This function has one parameter (txt), which corresponds to the parameter (this) used by the previous text box to call the function, that is, itself.

This is the end of this article about the detailed explanation of JavaScript onblur and onfocus events. For more relevant js onblur and onfocus event content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Commonplace onBlur event and onfocus event (js)
  • Introduction to the application of onblur and onfocus events in Js
  • Local refresh verification code implemented by jsp+ajax (onblur event triggers verification)

<<:  VMware virtual machine three connection methods example analysis

>>:  Detailed explanation of query examples within subqueries in MySql

Recommend

MySQL high availability solution MMM (MySQL multi-master replication manager)

1. Introduction to MMM: MMM stands for Multi-Mast...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

The principle and implementation of two-way binding in Vue2.x

Table of contents 1. Implementation process 2. Di...

Example of using negative margin to achieve average layout in CSS

For evenly distributed layouts, we generally use ...

Install mysql offline using rpm under centos 6.4

Use the rpm installation package to install mysql...

Detailed tutorial on installing Docker on Windows

Since my local MySQL version is relatively low, I...

How to use Vue-router routing

Table of contents 1. Description 2. Installation ...

How to implement navigation function in WeChat Mini Program

1. Rendering2. Operation steps 1. Apply for Tence...

Automatically load kernel module overlayfs operation at CentOS startup

To automatically load kernel modules in CentOS, y...

About Vue virtual dom problem

Table of contents 1. What is virtual dom? 2. Why ...

vue uses Ele.me UI to imitate the filtering function of teambition

Table of contents Problem Description The general...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

How to adjust the log level of nginx in Docker

Table of contents Intro Nginx Dockerfile New conf...

MYSQL performance analyzer EXPLAIN usage example analysis

This article uses an example to illustrate the us...