This is my code:
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) {
field.value = field.value.substring(0, 160);
field.blur();
field.focus();
return false;
} else {
countfield.value = maxlimit - field.value.length;
}
}
How can I display how many characters are remaining from a certain text box with a limit of 160?
let textArea = document. getElementById("textbox"); let characterCounter = document. getElementById("char_count"); const maxNumOfChars = 100; The textArea will be used to add an event that will activate the logic function.
To set the maximum character limit in input field, we use <input> maxlength attribute. This attribute is used to specify the maximum number of characters enters into the <input> element. To set the minimum character limit in input field, we use <input> minlength attribute.
You can achieve this using jQuery with the following simple code: var theCounter = $('#textareaLength'), textarea = $('#myTextarea'), maxLength = textarea. attr('length'); theCounter. text('0 / '+maxLength); theCounter.
Dynamic HTML element functionThe code in here with a little bit of modification and simplification:
<input disabled maxlength="3" size="3" value="10" id="counter">
<textarea onkeyup="textCounter(this,'counter',10);" id="message">
</textarea>
<script>
function textCounter(field,field2,maxlimit)
{
var countfield = document.getElementById(field2);
if ( field.value.length > maxlimit ) {
field.value = field.value.substring( 0, maxlimit );
return false;
} else {
countfield.value = maxlimit - field.value.length;
}
}
</script>
Hope this helps!
tip:
When merging the codes with your page, make sure the HTML elements(textarea
, input
) are loaded first before the scripts (Javascript functions)
You can bind key press event with your input box and returning false
if characters are more than 160 will solve the problem jsfiddle.
JavaScript:
$('textarea').keypress(function(){
if(this.value.length > 160){
return false;
}
$("#remainingC").html("Remaining characters : " + (160 - this.value.length));
});
HTML
<textarea></textarea>
<span id='remainingC'></span>
Included below is a simple working JS/HTML implementation which updates the remaining characters properly when the input has been deleted.
Bootstrap and JQuery are required for the layout and functionality to match. (Tested on JQuery 2.1.1 as per the included code snippet).
Make sure you include the JS code such that it is loaded after the HTML. Message me if you have any questions.
$(document).ready(function() {
var len = 0;
var maxchar = 200;
$( '#my-input' ).keyup(function(){
len = this.value.length
if(len > maxchar){
return false;
}
else if (len > 0) {
$( "#remainingC" ).html( "Remaining characters: " +( maxchar - len ) );
}
else {
$( "#remainingC" ).html( "Remaining characters: " +( maxchar ) );
}
})
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="row">
<div class="col-sm-6 form-group">
<label>Textarea</label>
<textarea placeholder="Enter the textarea input here.. (limited to 200 characters)" rows="3" class="form-control" name="my-name" id="my-input" maxlength="200"></textarea><span id='remainingC'></span>
</div>
</div> <!--row-->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With