FBJS textarea countdown
I have a textarea element in a Facebook application I want to limit, visually, to a certain number of characters. This is a code snippet I got from the web:
<script>
function textCounter(textarea, countdown, maxlimit)
{
textareaid = "ta1";
if (textareaid.value.length > maxlimit)
textareaid.value = textareaid.value.substring(0, maxlimit);
else
document.getElementById(countdown).value = '('+(maxlimit-textareaid.value.length)+' characters available)';
}
</script>
<form>
&l开发者_开发技巧t;textarea id="ta1" name="ta1" rows=5 cols=20
onKeyDown="textCounter('ta1','ta1count',100);"
onKeyUp="textCounter('ta1','ta1count',100);"
></textarea>
<br/>
<input id="ta1count" readonly type="text" size="30"/>
</form>
<script type="text/javascript">
textCounter('ta1','ta1count',100);
</script>
This script works well outside of a Facebook frame, but I don't understand the limitations of FBJS and what I'd need to change to make this script work. Has anyone had success implementing a similar feature?
Thanks.
You code snippet you posted doesn't work.
here is a version that does work (is tested)
<html>
<head>
<title>Tarea test</title>
<script type="text/javascript">
function id (e) {
return document.getElementById(e);
}
window.onload = function () {
var oElement = id('message');
var iMaxChars = 300;
oElement.onblur = getHandler(iMaxChars);
oElement.onfocus = getHandler(iMaxChars);
oElement.onchange = getHandler(iMaxChars);
oElement.onkeyup = getHandler(iMaxChars);
updateTextArea('message', iMaxChars);
}
function getHandler(iMaxChars) {
return function () { updateTextArea('message', iMaxChars) };
}
function updateTextArea(sTextAreaID, iMessageMaxLength) {
var oTextAreaNode = id(sTextAreaID);
var oMessageCounter = id('messagecounter');
var sMessageValue = oTextAreaNode.value;
var iMessageLength = sMessageValue.length;
if (iMessageLength > iMessageMaxLength) {
oTextAreaNode.value = sMessageValue.substr(0, iMessageMaxLength);
iMessageLength=iMessageMaxLength;
}
oMessageCounter.innerHTML = ""+(iMessageMaxLength - iMessageLength);
}
</script>
</head>
<body>
<form action="#" method="post">
<fieldset>
<label for="message" id="messagelabel">Message <span id="messagecounter">300</span></label>
<textarea id="message" name="message" rows="10" cols="50"></textarea>
</fieldset>
</form>
</body>
</html>
In you example you are trying to get a value from a string. textareaid.value
textareaid isn't a node in this scope.
精彩评论