0

I was having a look at various methods of using a character count in jQuery and found one and modified it and implemented it but I am having a bit of an issue with it. Just wondering if you guys could give me any pointers as to where I may be going wrong.

This is what I have for the code:

function countChar(val) 
{
    var len = val.value.length;

    if (len >= 500)
    {
        val.value = val.value.substring(0, 500);
        $('.stat').text(0);
    }
    else 
    {
        $sv = (500 - len);        
        $('.stat').text($sv);
        if ($sv <= 20)
        {           
            $('.stat').css({'border': '1px solid #C33', 'color': '#C33', 'background-color': '#F6CBCA'});
        }
        else if ($sv >= 21 && $sv <= 150)
        {
            $('.stat').css({'border': '1px solid #9F6000', 'color': '#9F6000', 'background-color': '#FEEFB3'});
        }
        else
        {
            $('.stat').css({'border': '1px solid #090', 'color': '#090', 'background-color': '#DFF2BF'});
        }

    }
}

countChar($('#notes').get(0));
$('#notes').keyup(function() {
    countChar(this);
});

My ('#notes') is just a normal textarea.

I have a fiddle for it here:

It is based on the question found here

When I implement it on my page, it wasn't showing up. Upon looking at firebug, it is giving me the following error:

TypeError: val is undefined

From my understanding, this error means you need to declare it using a var.

I have done this and then I get the following error:

TypeError: val.value is undefined

I did try doing a var on val.value but that threw up a syntax error.

I did try using var val in the countChar() and it gives the following error:

SyntaxError: missing formal parameter

I just can't seem to see what I am missing as it works in the fiddle just the way I would like it to.

Any pointers would be much appreciated. Thanks in advance.

Community
  • 1
  • 1
Chris
  • 431
  • 1
  • 5
  • 16

1 Answers1

0

The issue appears to be that I had put it outside of document.ready without even realising that I had. It works like a charm now

Chris
  • 431
  • 1
  • 5
  • 16