Character counter for innerHTML of all DIV's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tmeers
    New Member
    • Mar 2007
    • 16

    Character counter for innerHTML of all DIV's

    Ok I've found many script on how to count the number of characters you have typed into one text box and even ones that tell you how many characters you have left to type. But I have a classic ASP app that I need to count all the characters in every DIV element that is using innerHTML.

    I'm really not sure how to explain it any better but maybe even if some one could share some wisdom as to how to count it for all text box's perhaps I could mod it to fit my needs.

    Thanks for any help,
    Tim
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    could you post a html-example and note whether there could be divs in divs or not? should html-markup that is eventually included stripped from the count?

    kind regards

    Comment

    • tmeers
      New Member
      • Mar 2007
      • 16

      #3
      Originally posted by gits
      could you post a html-example and note whether there could be divs in divs or not? should html-markup that is eventually included stripped from the count?

      kind regards
      Actually the user would click on a div like so:
      [HTML]<div id="divSample" >
      </div>
      [/HTML]
      And it would fire the following event on click:
      [CODE=javascript] function captureObject()
      {
      currentObject = window.event.sr cElement;
      replaceFrame();
      window.event.ca ncelBubble = true;
      }
      function replaceFrame()
      {
      closeFrame();
      myFrame.documen t.body.innerHTM L = currentObject.i nnerHTML;
      document.all.my Frame.style.top = currentObject.o ffsetTop;
      document.all.my Frame.style.lef t = currentObject.o ffsetLeft;
      document.all.my Frame.style.wid th = currentObject.o ffsetWidth;
      document.all.my Frame.style.hei ght = currentObject.o ffsetHeight;
      document.all.my Frame.style.dis play = '';
      myFrame.documen t.body.style.ba ckgroundColor=' #f0ffff';
      myFrame.documen t.body.focus();
      }[/CODE]

      Any HTML markup should stay in the box. No div's would be in the div's.
      Thanks
      Tim
      Last edited by gits; Mar 14 '08, 04:20 PM. Reason: added code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        so you could use something like that:

        [CODE=javascript]function count_chars_in_ nodes(tag_name) {
        var nodes = document.getEle mentsByTagName( tag_name);
        var count = 0;

        for (var i = 0, n; n = nodes[i]; i++) {
        count += n.innerHTML.len gth;
        }

        return count;
        }[/CODE]
        kind regards

        Comment

        • tmeers
          New Member
          • Mar 2007
          • 16

          #5
          Yes that will work for on instance on a box, but I need to count all of the boxes on the page together into one total count. So div + div + div...... Not sure if it's possible but like i've heard before "any thing is possible with programming".

          But thank you for that, that alone will help me with another idea I just thought of for another project.

          Thanks for helping me by the way.
          Tim

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            when you put in 'div' as tag_name it goes to all divs ... so it should work for you ...

            kind regards

            Comment

            • tmeers
              New Member
              • Mar 2007
              • 16

              #7
              Ok I get what your saying, some times I'm not the quickest to catch on to these things after all. So I put in the body tag an onKeyUp event to call the function. And I know it's calling it because I'm getting an error from it saying that: 'Error: 'div' is undefined.

              Any ideas?

              Thanks again,
              Tim

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                your call should look like this:

                [CODE=javascript]count_chars_in_ nodes('div');[/CODE]
                may be you used:

                [CODE=javascript]count_chars_in_ nodes(div);
                [/CODE]
                ? ... :)

                kind regards

                Comment

                • tmeers
                  New Member
                  • Mar 2007
                  • 16

                  #9
                  Hmm it's still not working, no error and no counting.

                  Perhaps I will give it another go when I'm a "nice and rested" on monday.

                  Thanks a bunch,
                  Tim

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    here is a working example with the code:

                    [HTML]<html>
                    <script type="text/javascript">
                    function count_chars_in_ nodes(tag_name) {
                    var nodes = document.getEle mentsByTagName( tag_name);
                    var count = 0;

                    for (var i = 0, n; n = nodes[i]; i++) {
                    count += n.innerHTML.len gth;
                    }

                    return count;
                    }
                    </script>
                    <body onload="var c = count_chars_in_ nodes('div'); alert(c);">
                    <div>test</div>
                    <div>test1</div>
                    </body>
                    </html>
                    [/HTML]
                    kind regards

                    Comment

                    Working...