#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">
JavaScript:
object.onchange=function(){SomeJavaScriptCode};
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>
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!