onKeyUp / onKeyDown on Textbox displaying "X characters remaining in label" using javascript.

Greetings.. and thank you for using time to read my post..

I have a keyHandler.js file in my web-project where i have this function :

function CountLeft(field, count, max)
{
    var target = $get(count);
   
    if (field.value.length > max) field.value = field.value.substring(0, max);
    else target.value = max - field.value.length;
}  

.... i want to use this function on basically all textboxes in my webproject and are trying to call the function this way :

<asp:TextBox ID="txtTitle" runat="server" Width="100%" onKeyDown='CountLeft(this, "<%# lblTitleCL.ClientID %>", 50)' /><br />
<asp:Label ID="lblTitleCL" runat="server" /><asp:Label runat="server" ID="lblTitleCLDesc" Text=" characters remaining." />

... so to sum things up... lblTitleCL should display the total available characters. With no characters in txtTitle it should display "50".. on each onKeyDown it should call the CountLeft function, and display the correct amount of characters left in the label.

One problem.. is that i cannot seem to get the lblTitleCL control with this function.. am i declaring it the correct way in the function call ?

And.. how do i return the remaining characters to the label again ?

Cheers.. and thanx in advance.

Nygter.
NygterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NygterAuthor Commented:
I have also tried this one with no result :

<asp:TextBox ID="txtTitle" MaxLength="100" runat="server" Visible="False" Width="100%" onKeyUp="textCounter(txtMessage, this.form.myCounter, 250);" onKeyDown="textCounter(txtMessage, this.form.myCounter, 250);" />
<span id="myCounter">250</span> remaining characters.

function textCounter(field, countfield, maxlimit)
{
if (field.value.length > maxlimit)
   field.value = field.value.substring(0, maxlimit);
else
   countfield.value = maxlimit - field.value.length;
}

0
NygterAuthor Commented:
This one is very close to working, except that when the total number of allowed characters (maxLen) is reached.. it still counts downwards.. aka.. the substring doesn't work :
if (len > maxLen)
            {
                control.innerHTML = txt.substring(0, maxLen);

it should stop, and remove the latest character if it goes to zero. (0).


function countDown(control, maxLen, counter, typeName)
{
    var len = control.value.length;
    var txt = control.value;
    var span = document.getElementById(counter);
    span.style.display = '';
    span.innerHTML = (maxLen - len) + ' gjenstående tegn...';
    if (len >= (maxLen - 10)) 
    {
        span.style.color = 'red';
            if (len > maxLen) 
            {
                control.innerHTML = txt.substring(0, maxLen);
                //span.innerHTML = (maxLen - control.value.length) + ' gjenstående tegn...';
                //field.value = field.value.substring(0, maxlimit);
 
            }
    } 
    else 
    {
        span.style.color = ''; 
    }
}

Open in new window

0
Michel PlungjanIT ExpertCommented:
span.innerHTML = ( (maxLen - len>0)?(maxLen - len):0) + ' gjenstående tegn...';

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

NygterAuthor Commented:
Thank you for the answer mplungjan, but the only thing that doesn't work (which has to work) is that i can still type in the field. The Counter stops at zero now, but i can still type ahead after the limit has been reached.

Know how ?

Terje.
0
NygterAuthor Commented:
which means... this one has to trigger some way :

txt = txt.substring(0, maxLen);
0
NygterAuthor Commented:
on textbox which has a "maxlength" property this works ofcourse.. but a mulit-line textbox which is rendered as a <textarea> i need to have this in the javascript..
0
Michel PlungjanIT ExpertCommented:

<script>
function countDown(control, maxLen, counter, typeName) {
  var len = control.value.length;
  var txt = control.value;
  var span = document.getElementById(counter);
  span.style.color = ''; 
  span.style.display = '';
  diff = maxLen - len;
  span.innerHTML = ((diff > 0)?diff:0) + ' gjenstående tegn...';
  if (len < maxLen-10 ) return true; // no need to test
  span.style.color = 'red';
  if (len > maxLen) {
    control.value = txt.substring(0, maxLen);
    return false
  } 
  return true
}
</script>
<input type="text" onKeyUp="return countDown(this,20,'myCounter')">
<span id="myCounter">20 gjenstående tegn...</span>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NygterAuthor Commented:
Wicked work mplungjan !! :) that worked awfully good !

A true genius you are :)
0
NygterAuthor Commented:
Thanx a bunch for clearing out my headache. :)
0
Michel PlungjanIT ExpertCommented:
:)
we could also assign a keyhandler so you could not even type the max+1 character, but the above will work too
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.