javascript - Write how manu characters left in textbox -


i have textbox max value of 10, , want #pid p tag write number of remaining characters.

html:

<textarea id="content" maxlength="10"></textarea> <p id="pid">max 10 chars</p> 

javascript:

var content = document.getelementbyid('content');  var pid = document.getelementbyid('pid');  function charsleft() {      (var = 10; >= 1; i--) {      contentcount = 10;      pid.innerhtml = parseint(contentcount - 1);      }  }  content.onkeypress = charsleft; 

all managed when start typing, see 9, keep typing stays 9.

fiddle

i want pure js, no libraries.

function charsleft() {     pid.innerhtml = content.value.length > 0 ? content.getattribute("maxlength") - content.value.length + " chars left" : "max 10 chars"; } content.onkeyup = charsleft; 

see demo.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -