I am using the below code to set the character count on a textarea. This solution is working fine as far as i am passing the textarea ID.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var charactersAllowed = 255; // this is picked from widget configuration
$("#charCount").html(charactersAllowed);
$("#message").keyup(function() {
$("#charCount").html(charactersAllowed - ($(this).val().length));
if($(this).val().length > charactersAllowed) {
this.value = this.value.substring(0, charactersAllowed);
$("#charCount").html(charactersAllowed - ($(this).val().length));
}
});
});
</script>
</head>
<body>
<textarea name="message" id="message" tabindex="4" rows="4" cols="35"></textarea>
<p class="character_limit">Character limit: <span id="charCount"></span></p>
</body>
</html>
what i need to do is to wrap this functionality in function so that i can call this function on any input element. Can i wrap the same code inside a function name and call the same function on Textarea onchange() event?
Please provide me the inputs & help to recode this snippet.
Thanks Lokesh Yadav