473,804 Members | 3,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Character counter for innerHTML of all DIV's

16 New Member
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 5149
gits
5,390 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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
Mar 14 '08 #10

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

Similar topics

2
4488
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" WIDTH=352 HEIGHT=288 CLASSID="CLSID:C79D3167-6133-4e7c-821C-5C114611022D" CODEBASE="http://www.ipvideocenter.com/components/CameraControl.cab#VERSION= 3,2,1,0">
16
13348
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 in the context of developing an auto-suggest (basically reverse-engineering Google Suggest), but I don't see how any of the other code has anything to do with it. I *pretty sure* that I've narrowed it down to 1 line (the line on which the...
1
6776
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 World" Has anyone ever heard of a function that can do that? Thanks!
17
34745
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 fine. However the HTML have a SCRIPT tag that the browser should process, but
1
4589
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 javascript file, but now I am going to go completely AJAX & using innerHTML to display the same information to the browser. First, snippets of the original code that was working with no problem: XML first: <title>bla bla</title> ...
8
10114
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 pic and displays:block and visiblity:visible....and also changes pic2.. When the webpage is loaded it displays:none and visivlity:hidden also Heres my code:
7
1152
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 like 12/123. I tried to display the counter value is label but in vain. It seems like label is not rendered until the click event finishes it code. Any help is appreciated. Regards,
10
1901
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 --> <script type="text/javascript"> /* add 1 to the total */
0
9589
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.