# Shortening text in repeater

**URL:** <https://forum.wixstudio.com/t/shortening-text-in-repeater/14691>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [January 2, 2019, 4:37pm UTC](https://forum.wixstudio.com/t/shortening-text-in-repeater/14691 "2019-01-02T16:37:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drewfabrikant](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/drewfabrikant/32/7857_2.png) [@drewfabrikant](https://forum.wixstudio.com/u/drewfabrikant)\
**Post date:** [January 2, 2019, 4:37pm UTC](https://forum.wixstudio.com/t/shortening-text-in-repeater/14691/1 "2019-01-02T16:37:38Z")

</div>

@massasalah18  
I’m trying to shorten the text box within the repeater, but having issues. We don’t need the “see more” button, as we’ll link the full text to a new page.

[https://www.trustscout.com/ask-a-real-estate-expert](https://www.trustscout.com/ask-a-real-estate-expert)

let fullText; // variable to hold the full text let shortText; // variable to hold the short version of the text  
$w(" [#dynamicDataset](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.dynamicDataset) “).onReady( function () { // how many characters to include in the shortened version const shortTextLength = 40; // set the fullText variable to be the text from the collection fullText = $w(’ [#dynamicDataset](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.dynamicDataset) ').getCurrentItem().textField; // if no text to display, collapse the text element and the button if (!fullText) { $w(” [#repeater1](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.repeater1) “).forEachItem( (’ [#text349](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.text349) ‘)) .collapse(); $w(’ [#button69](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.button69) ').collapse(); } **else** { // if the text has fewer or the same number of characters as shortTextLength characters, display it as is and collapse the “Show More” button if (fullText.length \<= shortTextLength) { $w(” [#repeater1](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.repeater1) “).forEachItem( (’ [#text349](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.text349) ‘)).text = fullText; $w(’ [#button69](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.button69) ').collapse(); } **else** { // create the shortened version of the text and display it in the text element shortText = fullText.substr(0, shortTextLength) + “…”; $w(” [#repeater1](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.repeater1) ").forEachItem( (’ [#text349](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.text349) ')).text = shortText; } } });

---

<div class="post-metadata">

**Author:** ![massasalah18](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/massasalah18/32/876_2.png) [@massasalah18](https://forum.wixstudio.com/u/massasalah18)\
**Post date:** [January 3, 2019, 8:54am UTC](https://forum.wixstudio.com/t/shortening-text-in-repeater/14691/2 "2019-01-03T08:54:40Z")

</div>

Hello Drew

I have checked your website ( I assumed the page is Q\_and\_A(All)… right? ). As i understood, you want the text box inside your repeater to show maximum 40 characters. you have got the idea right but the code implementation was wrong.

in order to achieve that goal First you need to start with repeater for each item to loop over all the repeater items, then you check the text length of the items… here how it goes:

```javascript
$w.onReady(function () {
    $w("#dynamicDataset").onReady(function () {

    const shortTextLength = 40;
    let fullText;
    let shortText;

       $w("#repeater1").forEachItem(($w,item) => {
 
            fullText = $w('#dynamicDataset').getCurrentItem().answer;

            if (!fullText.length) {
                $w('#text401').collapse();
            } else
            if (fullText.length <= shortTextLength) {
                $w('#text401').text = fullText;

            } else {
                shortText = fullText.substr(0, shortTextLength) + "...";
                $w('#text401').text = shortText;
            }

        })

    });
 });

```

Best  
Massa

---

<div class="post-metadata">

**Author:** ![drewfabrikant](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/drewfabrikant/32/7857_2.png) [@drewfabrikant](https://forum.wixstudio.com/u/drewfabrikant)\
**Post date:** [January 3, 2019, 12:47pm UTC](https://forum.wixstudio.com/t/shortening-text-in-repeater/14691/3 "2019-01-03T12:47:08Z")

</div>

Thank you so much, @massasalah18 ! This worked perfectly.

---

<div class="post-metadata">

**Author:** ![jalalbaydounlb](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/jalalbaydounlb/32/10299_2.png) [@jalalbaydounlb](https://forum.wixstudio.com/u/jalalbaydounlb)\
**Post date:** [March 20, 2019, 8:44pm UTC](https://forum.wixstudio.com/t/shortening-text-in-repeater/14691/4 "2019-03-20T20:44:43Z")

</div>

Salam @massasalah18 ,  
Here is what i’m receiving after implementing the code, could you please assist?  
Thank you,  
Jalal

 ![](https://us1.discourse-cdn.com/wix/original/3X/9/9/9990ead58217a593011c0add95003be5a4e686a4.jpeg)

---

<div class="post-metadata">

**Author:** ![lbuhlnielsen](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/lbuhlnielsen/32/10195_2.png) [@lbuhlnielsen](https://forum.wixstudio.com/u/lbuhlnielsen)\
**Post date:** [October 12, 2020, 8:52pm UTC](https://forum.wixstudio.com/t/shortening-text-in-repeater/14691/5 "2020-10-12T20:52:51Z")

</div>

@jalal - you have to replace the “getCurrentItem (). answer” with the field name of the text in your collection you want to display, so eg if your field name is “job”, then it becomes

getCurrentItem (). job
