Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey guys.
i'm still trying to implement a quick char counter for a text area. problem is its not working even after changes i made instead of .innerHTML i worte .textContent
and insted of document.all['control'] i used getElementByID.
i'm attaching the code here, please help me get this working in other browsers except IE .

VB
function counter(method,cnt)    //accepts lang method and calc num of msgs
    {
        switch(method)
        {
            case 1 :
                if(cnt > 0 && cnt <= 70)
                    document.all('msgCounter').textContent = "1";
                if(cnt > 70 && cnt <= 134)
                    document.all('msgCounter').innerHTML = "2";
                if(cnt > 134 && cnt <= 201)
                    document.all('msgCounter').innerHTML = "3";
                if(cnt > 201 && cnt <= 268)
                    document.all('msgCounter').innerHTML = "4"; //tmp - need to determine limitation for msgs
                break;
            case 2 :
                if(cnt > 0 && cnt <= 160)
                    document.all('msgCounter').innerHTML = "1";
                if(cnt > 160 && cnt <= 306)
                    document.all('msgCounter').innerHTML = "2";
                if(cnt > 306 && cnt <= 459)
                    document.all('msgCounter').innerHTML = "3";
                break;
        }
        document.getElementById('chrCounter').innerHTML = cnt;
    }
Posted
Comments
Sergey Alexandrovich Kryukov 1-Apr-12 12:37pm    
What is these two "methods"? What are those ranges 0..70, 134..201, etc? Where do you count characters? return result?
--SA
Sergey Alexandrovich Kryukov 1-Apr-12 22:40pm    
And please do not re-post. I was supposed to remove this question and reply to your similar question:
http://www.codeproject.com/Questions/356408/using-javascript-to-count-chars-in-a-textarew-is-n

I did not do it by mistake. I also cannot see why did you ignore the advice by Peter_in_2780...
--SA
moses12345 2-Apr-12 6:36am    
function langChk()
{
var str = new String(document.getElementById('txt2').innerText);
var i = 0;
var cnt = str.length;
if(cnt == 0)
{
document.all('msgCounter').innerHTML = "0"
document.all('chrCounter').innerHTML = "0"
}

for(i=0 ; i < cnt ; i++) // will not start unless cnt > 0
{
var ch = parseInt(str.charCodeAt(i));

if(ch >= 1488 && ch <=1514) //chk 4 heb
{
counter(1,cnt);
break;
}
else
counter(2,cnt);
}
}

function counter(method,cnt) //accepts lang method and calc num of msgs
{
switch(method)
{
case 1 :
if(cnt > 0 && cnt <= 70)
setTextContent(document.getElementById('msgCounter'),"1");
if(cnt > 70 && cnt <= 134)
setTextContent(document.getElementById('msgCounter'),"2");
if(cnt > 134 && cnt <= 201)
setTextContent(document.getElementById('msgCounter'),"3");
if(cnt > 201 && cnt <= 268)
setTextContent(document.getElementById('msgCounter'),"4");//tmp - need to determine limitation for msgs
break;
case 2 :
if(cnt > 0 && cnt <= 160)
setTextContent(document.getElementById('msgCounter'),"1");
if(cnt > 160 && cnt <= 306)
setTextContent(document.getElementById('msgCounter'),"2");
if(cnt > 306 && cnt <= 459)
setTextContent(document.getElementById('msgCounter'),"3");
break;
}
//document.getElementById('chrCounter').innerText = cnt;
setTextContent(document.getElementById('chrCounter'),cnt);
}

function setTextContent(element, text)
{
while (element.firstChild!==null)
element.removeChild(element.firstChild); // remove all existing content
element.appendChild(document.createTextNode(text));
}

1 solution

moses12345 wrote:
…insted of document.all['control'] I used getElementByID
No, you did not. Did you see you own code? You still use document.all, which is not a legal or standard property. Also, you calculate in several places, but you should calculate it only once. You can calculate the DOM element (textarea control, in this case), in the very first line of the function counter or pass it as a parameter of this function, and calculate the control reference once, in the caller, using document.getElementById (which is not document.getElementByID, look thoroughly).

You also need to answer the questions I asked in my comment to the question, first of all, answer for yourself. You need to add a loop, iterate throw the control's value, increment some character counter. You also need to return the counter value. You don't try to do anything of the above.

—SA
 
Share this answer
 
Comments
Peter_in_2780 1-Apr-12 22:14pm    
I was going to say: What part of "Don't use document.all" didn't you understand?

Peter
Sergey Alexandrovich Kryukov 1-Apr-12 22:42pm    
Do you mean, saying this to OP? Yes, I remember you did correctly advised that, but your advice was ignored.
It looks like OP get advices aggressively, looking at his response to my answer to his previous question.
Now, I'm expecting down-vote or something like that... :-)

Maybe I should not have answered... :-)
--SA
Peter_in_2780 1-Apr-12 22:54pm    
Yeah, sorry. I meant saying to OP. Have a much heavier 5 than his 1 if it comes. ;)
Sergey Alexandrovich Kryukov 1-Apr-12 23:07pm    
Ha-ha. Thank you, Peter.
--SA
moses12345 2-Apr-12 6:32am    
ok guys, i pasted the wrong code, but i actually changed it
still not working . what im trying to do, is on every keyup event calculate the chars in the tectarea and write this counter result in a label.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900