I made a script to count characters from form textfields but is now working. I put an alert inside of keyup function
to see if it counts correctly, but every time i
is 6 (the total of form elements). Outside of keyup function
it counts correctly. What could be the problem? I don't see any problems with the code. Also the console log is empty (no errors).
// Count characters
formElementsTitles = ['content_title', 'content_description', 'text_content', 'image_url', 'video_embed', 'aditional_info'];
formElementsLengths = ['100', '5000', '20000', '1000', '1000', '1000'];
for(i = 0; i < formElementsTitles.length; i++)
{
$('#'+formElementsTitles[i]).keyup(function() {
charactersMax = formElementsLengths[i];
charactersCurrent = $(this).val().length;
charactersRemaining = charactersMax - charactersCurrent;
if(charactersCurrent >= charactersMax)
{
$("#count_"+formElementsTitles[i]).html('<span class="content_count_chars_yellow">'+charactersRemaining+'</span> characters remaining');
}
else
{
$("#count_"+formElementsTitles[i]).html('<span class="content_count_chars_green">'+charactersRemaining+'</span> characters remaining');
}
});
}