jsviews

Get length of a string with data-link


I would like to display the length of the text in my textarea. But since I don't want to build an event handler for it, I would like to try to solve this with the data-link. Unfortunately I don't find a way to do this.

<textarea id="commentText" data-link="commentText()"></textarea>
<span><span id="commentTextLength">{^{>commentText().length}}</span>/300</span>

Solution

  • You can simply change the expression from commentText().length to commentText()^length.

    See "deep linking".

    In that way you make the expression update whenever there is an observable change in commentText() itself.

    So you can write:

    {^{>commentText()^length}}
    

    or

    <span data-link="commentText()^length"></span>
    

    This works equally whether you are using a plain string value commentText^length or a computed observable commentText()^length as in your example.