Problem - Creating an Expandable Text Box with a Show-More

I am following the Wix’s tutorial: https://support.wix.com/en/article/creating-an-expandable-text-box-with-a-show-more-link-using-wix-code. My amended version is written below:

let fullText; // variable to hold the full text
let shortText; // variable to hold the short version of the text

$w.onReady(function () {
 // how many characters to include in the shortened version
 const shortTextLength = 40;
 // read the full text and store it in the fullText variable
    fullText = $w("#text22").text;
 // // grab the number of characters defined in shortTextLength and store them in the shortText variable
    shortText = fullText.substr(0, shortTextLength) + "...";
 // set the contents of #text46 to be the short text
    $w("#text22").text = shortText;
});

However, I am constantly receiving an error message: 'TypeError: Cannot read property ‘substr’ of undefined ’


How can I solve this?

Thanks

Cheers,

Axel

Hi,
Is text22 connected to a dataset ?
If so, you should wrap the element with $w(‘#yourDataset’).onReady function.
For more information:

Roi.

Hi Axel, does ‘#text22’ have text? Could you try doing console.log(fulltext) to make sure the code can actually read that text?

I too am having this problem. I am not a coder. Please advise ASAP.