Hello there! I have created a character counter in JavaScript that looks like this:
<textarea class="SmsText" id="txttemplate" maxlength="160" />
<span id="charsCount">160</span></strong><span>character(s) left</span>
$(document).ready(function() {
keypressed = false;
$('#txttemplate').keypress(function (e) {
keypressed = true;
});
$('#txttemplate').keyup(function () {
counter($(this));
});
$('#txttemplate').keydown(function () {
counter($(this));
});
});
function counter(obj) {
var max = obj.attr('maxlength');
var valLen = obj.val().length;
obj.val(obj.val().substring(0, max));
$('#charsCount').text(max - valLen);
}
Currently, my code treats the "enter" key (13) as a single character, while the maxlength attribute of the textarea counts it as 2 characters. How can I modify my code to count two characters when "enter" or "carriage return" is pressed?