I am trying to use Jquery to count the characters in a text area, here is what i have so far..
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#textfield").on('keyup, paste', function(){
var Characters = ("#textfield").val().replace(/(<([^>]+)>)/ig,"").length;
$("#counter").text("Characters left: " + (1500 - Characters));
});
});
</script>
</head>
<body>
<form id="input_form" method="POST" action="?">
<textarea id="textfield"></textarea>
</form>
<div id="counter"></div>
</body>
</html>
Nothing is being output into my div... Any ideas why?
You have two issues in your code
1) you do not need comma while passing the event list/multiple events to on method. it should be .on('keyup paste'
2) You are missing jquery selector while getting text value of textarea. you should use
$("#textfield").on('keyup, paste', function(){
var Characters = $("#textfield").val().replace(/(<([^>]+)>)/ig,"").length;
$("#counter").text("Characters left: " + (1500 - Characters));
});
Working Demo
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