Table of Contents
oninput event
onpropertychange event
The difference between the oninput event and the onpropertychange event:
Oninput is used in combination with onpropertychange
When using jQuery
Home Web Front-end JS Tutorial Detailed explanation of events triggered by changes in input tag content (with examples)

Detailed explanation of events triggered by changes in input tag content (with examples)

Nov 20, 2018 pm 03:32 PM
html5 javascript

This article brings you a detailed explanation of the events triggered by changes in the input tag content (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Native method

onchange event

<input type="text" onchange="onc(this)">
Copy after login
function onc(data){
    console.log(data.value);
}
Copy after login

The onchange event is triggered when the content changes and loses focus. That is, if the focus is lost and the content does not change, it will not be triggered. If the content changes but the focus is not lost, it will not be triggered in real time.
js does not trigger when directly changing the value value

oninput event

<input id="inp" type="text" oninput="inp(this)">
Copy after login
function inp(data) {
    console.log(data.value)
}
Copy after login

oninput event is triggered in real time when the input content changes. The oninput event is an event supported by most browsers except IE, and is triggered in real time when the value changes.
js is not triggered when the value is changed directly.

onpropertychange event

The onpropertychange event is triggered in real time. It will be triggered every time a character is added or deleted. This event will also be triggered by js changes, but this event is exclusive to IE.
When input is set to disable=true, it will not be triggered.

The difference between the oninput event and the onpropertychange event:

The onpropertychange event is triggered by any property change, but oninput is only triggered when the value changes. Oninput must be registered through addEventListener() , the onpropertychange registration method is the same as the general event.

Oninput is used in combination with onpropertychange

Oninput is a standard event of HTML5. It is used to detect content changes of textarea, input:text, input:password and input:search elements through the user interface. Very useful, it is triggered immediately after the content is modified, unlike the onchange event which needs to lose focus before it is triggered. The oninput event is not supported in versions below IE9 and needs to be replaced by the IE-specific onpropertychange event. This event will be triggered when the user interface changes or when the content is directly modified using a script. There are the following situations:

Modification The selected state of the input:checkbox or input:radio element is changed, and the checked attribute changes.
The value of the input:text or textarea element is modified, and the value attribute changes.
The selected item of the select element has been modified, and the selectedIndex attribute has changed.
After listening to the onpropertychange event, you can use the propertyName attribute of the event to get the changed property name.

The sample code for collecting oninput & onpropertychange to monitor input box content changes is as follows:

// Firefox, Google Chrome, Opera, Safari, Internet Explorer from version 9

function OnInput (event) {
    alert ("The new content: " + event.target.value);
}
Copy after login

/ / Internet Explorer

function OnPropChanged (event) {
    if (event.propertyName.toLowerCase () == "value") {
        alert ("The new content: " + event.srcElement.value);
    }
}

<input type="text" oninput="OnInput (event)" onpropertychange="OnPropChanged (event)" value="Text field" />
Copy after login

When using jQuery

, you only need to bind the oninput and onpropertychange events at the same time. The sample code is as follows:

$('textarea').bind('input propertychange', function() {
    $('.msg').html($(this).val().length + ' characters');
});
Copy after login

The last thing to note is: Both oninput and onpropertychange events have a small bug in IE9, that is, they are not triggered when content is deleted through the cut and delete commands in the right-click menu, but they are normal in other versions of IE.

The above is the detailed content of Detailed explanation of events triggered by changes in input tag content (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles