Skip to main content

I alwayalways do it like this:

$(document).ready(function(){ var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.on('keyup', function(e){ length = new Number(maxChars.val().length); counter = max_length-length; $("#sessionNum_counter").text(counter); }); } }); 
$(document).ready(function() { var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.on('keyup', function(e) { length = new Number(maxChars.val().length); counter = max_length - length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 
<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 

I alway do it like this:

$(document).ready(function(){ var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.on('keyup', function(e){ length = new Number(maxChars.val().length); counter = max_length-length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 

I always do it like this:

$(document).ready(function() { var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.on('keyup', function(e) { length = new Number(maxChars.val().length); counter = max_length - length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 
bind is deprecated
Source Link
mplungjan
  • 180k
  • 29
  • 183
  • 246

I alway do it like this:

$(document).ready(function(){ var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.bindon('keyup', function(e){ length = new Number(maxChars.val().length); counter = max_length-length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 

I alway do it like this:

$(document).ready(function(){ var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.bind('keyup', function(e){ length = new Number(maxChars.val().length); counter = max_length-length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 

I alway do it like this:

$(document).ready(function(){ var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.on('keyup', function(e){ length = new Number(maxChars.val().length); counter = max_length-length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span> 
Source Link
Sašo Krajnc
  • 133
  • 2
  • 3
  • 11

I alway do it like this:

$(document).ready(function(){ var maxChars = $("#sessionNum"); var max_length = maxChars.attr('maxlength'); if (max_length > 0) { maxChars.bind('keyup', function(e){ length = new Number(maxChars.val().length); counter = max_length-length; $("#sessionNum_counter").text(counter); }); } }); 

Input:

<input name="sessionNum" id="sessionNum" maxlength="5" type="text"> Number of chars: <span id="sessionNum_counter">5</span>