Home > Web Front-end > JS Tutorial > body text

How to use onchange event in JS?

Guanhui
Release: 2020-07-18 15:44:09
Original
10338 people have browsed it

How to use onchange event in JS?

#How to use the onchange event in JS?

The onchange event in JS will occur when the content of the field changes. It can also be used for events triggered after the radio button and check box change. There are two syntaxes. One is in the element. Add the "onchange" attribute to the attribute. The attribute value is JS code. The other way is to set it directly on the element object.

Sample code

HTML:

<element onchange="SomeJavaScriptCode">
Copy after login

JavaScript:

object.onchange=function(){SomeJavaScriptCode};
Copy after login

Complete example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文网(phpcn.com)</title>
</head>
<head>
<script>
function myFunction(){
	var x=document.getElementById("fname");
	x.value=x.value.toUpperCase();
}
</script>
</head>
<body>

输入你的名字: <input type="text" id="fname" onchange="myFunction()">
<p>当你离开输入框后,函数将被触发,将小写字母转为大写字母。</p>

</body>
</html>
Copy after login

Recommended tutorial:《 PHP

The above is the detailed content of How to use onchange event in JS?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!