<html>
<head>
<title>Brojac reci</title>
<script src="script.js" type="text/javascript"></script>
<style type="text/css">
h3 { color: gray; }
</style>
</head>
<body>
<form action="#" method="">
<textarea id="myText" rows="4" cols="50"></textarea><br />
<h3 id="textLengthInfo">0/16 chars used!</h3>
<input id="submit" type="submit" name="submit" value="Post!" />
</form>
</body>
</html>
window.onload = initAll;
//set maximum number of chars
var maxNumChars = 16;
function initAll() {
var textBox = document.getElementById("myText");
textBox.onkeyup = countChars;
}
function countChars() {
var textLengthInfo = document.getElementById("textLengthInfo");
var submitButton = document.getElementById("submit");
currentNum = this.value.length;
textLengthInfo.innerHTML = currentNum + "/" + maxNumChars +" chars used!";
if(currentNum > maxNumChars) {
textLengthInfo.style.color = "red";
textLengthInfo.style.fontWeight = "bold";
submitButton.disabled=true;
} else {
textLengthInfo.style.removeProperty("color");
textLengthInfo.style.removeProperty("fontWeight");
submitButton.disabled=false;
}
}
A uzivo mozete videti ovde:
http://pakito.iz.rs/javascript-lastfm-demo/
Sve je okej, osim te jedne stvari - kako da prebrojim karaktere kada korisnik pastuje neki tekst? Koji bi to event bio? Trazio sam po netu, i procitao sam na forumima da tako nesto ne postoji. Sa druge strane, znam da moze, jer na Twitteru to radi. Na last.fm-u ne.
Pokusao sam sa onChange eventom, mada to nije dobro, zato sto se detektuje promena tek nakon sto textarea izgubi fokus. Dakle, imate neki predlog?