最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - nicedit WYSIWYH editor + character count - Stack Overflow

matteradmin6PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far