Expandable Text Code Help

Hello, I’m trying to have text cut off at 40 characters in a repeater that’s linked to a dataset. (I dont actually need a show more button) This is my code so far:

$w.onReady( function () {
//TODO: write your page related code here…
$w(“#dynamicDataset”).onReady( function () {
const shortTextLength = 30;
fullText = $w(‘#dynamicDataset’).getCurrentItem().order;
if (!fullText) {
$w(‘#myTextElement’).collapse();
} else {
display it as is and collapse the “Show More” button
if (fullText.length <= shortTextLength) {
$w(‘#myTextElement’).text = fullText;
} else {
shortText = fullText.substr(0, shortTextLength) + “…”;
$w(‘#myTextElement’).text = shortText;
}
}
});
});

Does anyone know what I’m doing wrong? It keeps on showing the last element of my database for each container in the repeater.

Thanks

So don’t use this code if you don’t need the full text.
Basically all you have to do is:

$w("#text1").text = $w("#text1").text.substring(0,40);

and if it’s a repeater do this instead:

$w("#repeater1").forEachItem( ($item, itemData, index) => {
$item("#text1").text = $item("#text1").text.substring(0,40);
})

and of course, put it inside:

 $w.onReady(function () { 
//HERE
})

Thank you for the help! I was also wondering what I would need to add if the repeater was connected to a dataset? So far it works but only when its not connected.

Thanks!

Is the text you’re referring to, retrieved from the dataset?

Yea, sorry for not specifying before aha

So the last code I posted above should work

On second thought, I forgot to mention you should wrap it in dataset.onReady(). Like this:

 
$w.onReady(function () {
    $w("#dataset1").onReady( () => {
    $w("#repeater1").forEachItem( ($item, itemData, index) => {
$item("#text1").text = $item("#text1").text.substring(0,40);
})
    })
});

Ah, it works now! Thank you so much

You’re welcome