javascript - nicedit WYSIWYH editor + character count - Stack Overflow

I'm using nicedit on a textarea to allow rich editor functionality.I also want to add character c

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

So, 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613780a4636108.html

相关推荐

  • javascript - nicedit WYSIWYH editor + character count - Stack Overflow

    I'm using nicedit on a textarea to allow rich editor functionality.I also want to add character c

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信