admin管理员组文章数量:1435823
I'm using nicedit on a textarea to allow rich editor functionality. I also want to add character counter at the end of the textarea in order to let user type specific number of character in the textarea. I use the above code and the editor works great.
<script src=".js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
Thanks
I'm using nicedit on a textarea to allow rich editor functionality. I also want to add character counter at the end of the textarea in order to let user type specific number of character in the textarea. I use the above code and the editor works great.
<script src="http://js.nicedit./nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
Thanks
Share Improve this question asked Mar 8, 2012 at 15:09 heav3nheav3n 771 silver badge7 bronze badges3 Answers
Reset to default 4So, you just need to know the number of characters in a textarea?
You can get that with the textarea's value's length property:
textarea.value.length
Here's an example:
<textarea onkeyup="console.log(this.value.length)"></textarea>
Here's a char count check. It is not foolproof, but it will get you started:
<textarea onkeyup="updateCounter(this)" onpropertychange="this.onkeyup()"></textarea>
<div id="limit"></div>
<script>
var limit = document.getElementById("limit");
function updateCounter(textarea) {
var len = textarea.value.length;
var max = 10;
if(max - len < 0) {
textarea.value = textarea.value.substring(0, max);
} else {
limit.innerHTML = max - len;
}
}
</script>
Also, remember that Stack Overflow is a programmer help site. We'll help with coding troubles, but we don't just write programs for people (there's plenty of sites for that ;) ).
If you want a dynamic character counter for a NicEdit textarea, then you should realize that a NicEditor (and most/all WYSIWYG HTML editors) is an editable div, and the contents of the div are only added to the associated textarea after the div looses focus. I found that simply trying to dynamically count the chars in the textarea didn't work. Perhaps I did something wrong. In any case, I did get a dynamic NicEdit char counter working, as follows:
Setup the HTML page to use NicEdit:
<script src="http://js.nicedit./nicEdit-latest.js" type="text/javascript"></script> <script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
Add a reference to a JavaScript file which contains the counter function:
<script type="text/javascript" language="JavaScript" src="nicCount.js"></script>
The contents of nicCount.js are:
function nicCount(editor, ctrId, maxChars) { // Count characters remaining in the given nicEditor (editor), and update the given counter (ctrId) counterObj = document.getElementById(ctrId); if(counterObj) { var content = editor.getContent(); if(maxChars - content.length <= 0) { // if there are no characters remaining, display the negative count in red counterObj.innerHTML = "<font color='red'>"+ (maxChars - content.length) + "</font>"; } else { // characters remaining counterObj.innerHTML = maxChars - content.length; } } }
Define a textarea, making sure it has a unique ID:
<textarea name='mytextarea1' id='mytextarea1
rows=10 cols=100>`Depending on where you want the counter to appear, place the following HTML code above or below the textarea code. The actual counter value was also given a unique ID:
<p><b id='mycounter1'>1000</b> characters remaining</p>
After the textarea and counter lines, I added the following HTML code block, to dynamically invoke my :
<script type="text/javascript"> //<[!CDATA[ bkLib.onDomLoaded (function() { var editor = nicEditors.findEditor('mytextarea1'); editor.getElm().onkeyup = function() { nicCount(editor, 'mycounter1', 1000); } }) //]] </script>
Note that my char counter is really a chars-remaining counter (with a 1000-char limit), since I was interested in enforcing character limits on the textarea (and letting the user know how many character they could still add), but it can easily be adjusted to be an actual chars-used counter.
I tested this in FireFox 26 and IE9.
Had this problem too, after some search I came into this solution:
$('body').on('keyup keydown focus','.nicEdit-selected', function (event) { $('#chars_text').text($(this).html().replace(/<\/?[^>]+(>|$)/g, "").length)});
chars_text id can be eg. SPAN element placed anywhere near nicedit Other solutions were based on .text().length, nicEdit use html() and this brings another problem - chars count include also TAGS inserted by nicEdit, so you have to strip them first.
Hope this help.
本文标签: javascriptnicedit WYSIWYH editorcharacter countStack Overflow
版权声明:本文标题:javascript - nicedit WYSIWYH editor + character count - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745613779a2666273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论