Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jbowman99's avatar

[Suggestions] counting characters in a textarea

I am trying to count characters in a text area and then run a function on the count in order to produce a cost amount based on how many characters have been entered.

First: can't seem to get any scripts i write in my blades to function AT all. a little assistance on adding scripts to blades is appreciated.

Second: i've found several different JS and JQuery examples on counting characters didn't know if anyone had a favorite or one that works better than others.

0 likes
4 replies
christopher's avatar

You can do it with jQuery:

<script>

        // Text Counter

        (function($) {
            $.fn.extend( {
                limiter: function(limit, elem) {
                    $(this).on("keyup focus", function() {
                        setCount(this, elem);
                    });
                    function setCount(src, elem) {
                        var chars = src.value.length;
                        if (chars > limit) {
                            src.value = src.value.substr(0, limit);
                            chars = limit;
                        }
                        elem.html( limit - chars );
                    }
                    setCount($(this)[0], elem);
                }
            });
        })(jQuery);

        var elem = $("#chars");
        $("#text").limiter(40, elem);

    </script>

HTML

<span id="chars"></span>
<input name="name" id="text" type="text" >
jekinney's avatar

Can't edit by phone, but wanted to add you can use vue for request to API in PHP (laravel) that keeps the number going or use vue to count as the person's typing.

jbowman99's avatar

@jekinney I would like to count as the person is typing i'll look into vue

just cant seem to get the scripts to work for me Javascript in general doesn't seem to want to work

Please or to participate in this conversation.