A brief analysis of adding listener events when value changes in html input

A brief analysis of adding listener events when value changes in html input

The effect to be achieved
In many cases, we will monitor the changes in the input box value in real time so that we can take immediate actions to guide the browser to enhance the user experience of the website. For example, instantly display the number of bytes that have been entered into the input box, or instantly read the input value to guide the search, which is the associated search effect of Google.
We can do a lot if we can just capture the moment.
What you need to know
First, we need to understand the difference between onchange and onpropertychange :

In IE, when the properties of an HTML element change, they can be captured instantly through onpropertychange.
When the onchange attribute value changes, the current element must also lose focus (onblur) in order to activate the event.

After understanding this, we find that the effect of onpropertychange is what we want, but unfortunately, it only works under IE. Can we find another time to replace onpropertychange?

After reading the information, I learned that the oninput event can be used to achieve the same effect in other browsers . It's great. We just need to distinguish the IE browser.

Use of oninput

Next, let's take a look at how to use oninput.
If you write the registration time directly on the page, you can do it as follows:
<、input type="text" name="textfield" oninput="alert(this.value);" onpropertychange="alert(this.value)" />

However, when oninput is separated from the JS code, it is somewhat different from the normal event registration method and must be registered using addEventListener.

Difference between attachEvent and addEventListener

At this point, let's take a look at how to use attachEvent and addEventListener:

The attachEvent method attaches other processing events to an event. (Not supported on Mozilla series)
addEventListener method for Mozilla series

Example:

XML/HTML CodeCopy content to clipboard
  1. document.getElementByIdx_x_x("btn") .onclick = method1 ;
  2.   
  3. document.getElementByIdx_x_x("btn") .onclick = method2 ;
  4.   
  5. document.getElementByIdx_x_x("btn") .onclick = method3 ;

If written like this, only medhot3 will be executed

Write it like this:

XML/HTML CodeCopy content to clipboard
  1. var btn1Obj = document .getElementByIdx_x_x("btn1");
  2.   
  3. btn1Obj.attachEvent("onclick",method1);
  4.   
  5. btn1Obj.attachEvent("onclick",method2);
  6.   
  7. btn1Obj.attachEvent("onclick",method3);

The execution order is method3->method2->method1

If it is the Mozilla series, this method is not supported and addEventListener is needed

XML/HTML CodeCopy content to clipboard
  1. var btn1Obj = document .getElementByIdx_x_x("btn1");
  2.   
  3. btn1Obj.addEventListener("click",method1,false);
  4.   
  5. btn1Obj.addEventListener("click",method2,false);
  6.   
  7. btn1Obj.addEventListener("click",method3,false);
  8.   
  9. The execution order is method1- > method2- > method3

Now that we know how to use addEventListener to register the oninput event, let's go back to the problem we need to solve [dividing the browser].

Determine IE browser

How to distinguish IE?
This seems to be a common problem. There are many ways to find it on the Internet, which can be classified into two categories:
The first is to determine the functional attributes of the browser.
The second is to determine the traditional user-agent string, which may be the oldest and most popular detection method.
We won't go into detail here. We use a simpler method to judge

XML/HTML CodeCopy content to clipboard
  1. if("\v"=="v") {
  2.   
  3. alert("IE");
  4.   
  5. }else{
  6.   
  7. alert("NO");
  8.   
  9. }
  10.   

The problems we have encountered so far have been solved. Let's start writing code to test whether our ideas can be implemented.

The above brief analysis of html input and value change to add listening events is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

Original URL: http://www.web600.net/html/editor/JavaScript/201001131529.html

<<:  Tutorial on how to install and use Ceph distributed software under Linux

>>:  Measured image HTTP request

Recommend

MySQL implements a solution similar to Oracle sequence

MySQL implements Oracle-like sequences Oracle gen...

How to set background blur with CSS

When making some pages, in order to make the page...

How to install mysql6 initialization installation password under centos7

1. Stop the database server first service mysqld ...

How to achieve seamless token refresh

Table of contents 1. Demand Method 1 Method 2 Met...

CSS multi-column layout solution

1. Fixed width + adaptive Expected effect: fixed ...

Complete steps to quickly build a vue3.0 project

Table of contents 1. We must ensure that the vue/...

Mysql master-slave synchronization Last_IO_Errno:1236 error solution

What is the reason for the Last_IO_Errno:1236 err...

Installation steps of mysql under linux

1. Download the mysql tar file: https://dev.mysql...

Windows Service 2016 Datacenter\Stand\Embedded Activation Method (2021)

Run cmd with administrator privileges slmgr /ipk ...

Six important selectors in CSS (remember them in three seconds)

From: https://blog.csdn.net/qq_44761243/article/d...

Detailed explanation of the use of docker tag and docker push

Docker tag detailed explanation The use of the do...

Solve the problem that Mysql5.7.17 fails to install and start under Windows

Install MySQL for the first time on your machine....

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

Example of JSON output in HTML format (test interface)

To display the JSON data in a beautiful indented ...