473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Character counter for innerHTML of all DIV's

16
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
Mar 14 '08 #1
9 5116
gits
5,390 Expert Mod 4TB
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
Mar 14 '08 #2
tmeers
16
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:
Expand|Select|Wrap|Line Numbers
  1.    function captureObject()
  2.    {
  3.         currentObject = window.event.srcElement;
  4.                 replaceFrame();        
  5.         window.event.cancelBubble = true;
  6.    }
  7.    function replaceFrame()
  8.    {
  9.          closeFrame();         
  10.          myFrame.document.body.innerHTML = currentObject.innerHTML;
  11.          document.all.myFrame.style.top = currentObject.offsetTop;
  12.          document.all.myFrame.style.left = currentObject.offsetLeft;
  13.          document.all.myFrame.style.width = currentObject.offsetWidth;
  14.          document.all.myFrame.style.height = currentObject.offsetHeight;        
  15.          document.all.myFrame.style.display = '';
  16.          myFrame.document.body.style.backgroundColor='#f0ffff';
  17.          myFrame.document.body.focus();                 
  18.    }
Any HTML markup should stay in the box. No div's would be in the div's.
Thanks
Tim
Mar 14 '08 #3
gits
5,390 Expert Mod 4TB
so you could use something like that:

Expand|Select|Wrap|Line Numbers
  1. function count_chars_in_nodes(tag_name) {
  2.     var nodes = document.getElementsByTagName(tag_name);
  3.     var count = 0;
  4.  
  5.     for (var i = 0, n; n = nodes[i]; i++) {
  6.         count += n.innerHTML.length;
  7.     }
  8.  
  9.     return count;
  10. }
kind regards
Mar 14 '08 #4
tmeers
16
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
Mar 14 '08 #5
gits
5,390 Expert Mod 4TB
when you put in 'div' as tag_name it goes to all divs ... so it should work for you ...

kind regards
Mar 14 '08 #6
tmeers
16
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
Mar 14 '08 #7
gits
5,390 Expert Mod 4TB
your call should look like this:

Expand|Select|Wrap|Line Numbers
  1. count_chars_in_nodes('div');
may be you used:

Expand|Select|Wrap|Line Numbers
  1. count_chars_in_nodes(div);
  2.  
? ... :)

kind regards
Mar 14 '08 #8
tmeers
16
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
Mar 14 '08 #9
gits
5,390 Expert Mod 4TB
here is a working example with the code:

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

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

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
Mar 14 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Carl Gilbert | last post by:
Hi I'm having problems with the following code. All I want to do is keep a counter on the page for every new image recieved without deviating from the "for" and "event". <OBJECT ID="CamImage1"...
16
by: Joel Byrd | last post by:
I am having this ridiculous problem of trying to set the innerHTML of a div node. It works in all other browsers, but internet explorer is giving me an "Unknown runtime error". This is actually...
1
by: Jimbo | last post by:
Basically I'm looking for a function that will parse a string and replace any entity codes it finds w/ the actual character. For instance the function would convert "Hello&nbsp;World" into "Hello...
17
by: PJ | last post by:
Greetings... I have stumbled upon a small problem. I use Ajax to retrieve part of a page I need to update. I update a DIV element with the HTML contents I get from another page. It works...
1
by: Tarik Monem | last post by:
I have been able to successfully retrieve data from an xml file, where the data has been massaged a little bit, to create a table to be retrieved and it is displayed via a document.writeln within a...
8
Toxinhead
by: Toxinhead | last post by:
Hey there guys I am having a little trouble with InnerHTML as i can't seem to figure out how to set the style of my div tag..... What im trying to do is that when i rollover pic1 it changes that...
7
by: sajit | last post by:
Hi Folks, Right now I am working on eMail Marketing System. There I have a module which sends news letters to subscribers. The problem is I want to put a counter on a page to display sent mails...
10
by: mozambique43 | last post by:
I have this code I would like to use as a link counter but it just will not store the number nor increment it. please help. I need this ASAP. <!-- Start: JAVASCRIPT for Counter --> ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.