简体   繁体   中英

what the reason to function oninput in js is not work?

there are practical video uploaded by someone and i do step by step what do you write in video ( i mean codes ) but nothing is !

the used language ( html and js )

 var $count = document.getElementById("count"), $textarea = document.getElementById("text"), $maxlength = $textarea.getAttribute("maxlength"); $maxlength.oninput = function () { $count.innerHTML = $maxlength - this.value.length; };
 <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="cal.css"> <title>cal</title> </head> <body> <form action=""> <textarea name="text" id="text" cols="30" rows="10" maxlength="100"></textarea> <span id="count">100</span> </form> <script src="cal.js"></script> </body> </html>

Not $maxlength , use $textarea to listen oninput event.

 var $count = document.getElementById("count"), $textarea = document.getElementById("text"), $maxlength = $textarea.getAttribute("maxlength"); $textarea.oninput = function () { $count.innerHTML = $maxlength - this.value.length; };
 <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="cal.css"> <title>cal</title> </head> <body> <form action=""> <textarea name="text" id="text" cols="30" rows="10" maxlength="100"></textarea> <span id="count">100</span> </form> <script src="cal.js"></script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM