Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery text area character count

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?

like image 371
Brian Avatar asked Jul 13 '26 00:07

Brian


1 Answers

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

like image 195
Milind Anantwar Avatar answered Jul 15 '26 14:07

Milind Anantwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!