PDA

View Full Version : Problem with function and the call.


songboy
07-27-2013, 03:11 PM
I have a javascript function for a text area counter - works fine.
I tried a new approach to deal with the 'hit enter key' problem so that,
if the enter key is pressed, it deducts 50 from the counter. The need for this is to make sure that the text, plus the enter hits, will keep the text within a defined space on the web page. The deduct 50 character function also works - but only once. Worse still, if you add another character, the character left counter goes back to what the counter was, before 50 character were deducted. I've put the two functions together into another function so they can be called together. I've changed the order of the functions but when I do this, the enter key function does not work at all. I'd like to say that I have some idea what is happening but I don't. Any help would be much appreciated. Here's all the relevant code.

function check_length(writing_details)
{
maxLen = 700; // max number of characters allowed
if (writing_details.blog_text.value.length >= maxLen)
{
// Reached the Maximum length so trim the textarea
writing_details.blog_text.value = writing_details.blog_text.value.substring(0, maxLen);
}
else
{ // Maximum length not reached so update the value of my_text counter
writing_details.text_num.value = maxLen - writing_details.blog_text.value.length;}
}
function break_count(writing_details)
{
if(event.keyCode == 13)
{
writing_details.text_num.value = maxLen - writing_details.blog_text.value.length -50;
}
}
function textcount()
{
check_length(writing_details)
break_count(writing_details)
}

Here's the call:
<textarea onKeyDown=textcount(this.form); onKeyPress=textcount(this.form); name="blog_text" cols=50 rows=10></textarea>

Thanks - songboy