javascript - jQuery character count - function not being called -


i'm creating jquery function count amount of characters in text field , output number. not seem called on key release. page text field external page being included index.php , js being included external js file.

  $('#commentfield').keyup(function () {     alert("functioncalled");     var max = 500;     var len = $(this).val().length;     if (len >= max) {       $('#charnum').html(' have reached limit');     } else {       var char = max - len;       $('#charnum').text(char + ' characters left');     }   }); 

text area:

<input type="text" name="commentarea" placeholder="comment..." autofocus="autofocus" id="commentfield"/> 

the div populated:

<div id="charnum">here</div> 

the external js file being access fine function not seem called. why not working?

if dynamic content need call .on. however, change 'body' closer element in dom.

$('body').on('keyup','#commentfield',function () {     alert("functioncalled");     var max = 500;     var len = $(this).val().length;     if (len >= max) {       $('#charnum').html(' have reached limit');     } else {       var char = max - len;       $('#charnum').text(char + ' characters left');     } }); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -