Code error message when creating an expandable text box

Can anyone out there tell me why I get this error message when creating an expandable text box with a “show more” button: Wix code SDK error: The text parameter that is passed to the text method cannot be set to the value 40. It must be of type string.

Hi,

Without seeing your code it’s difficult to say for sure. Apparently you are trying to set the contents of the text box to a number and not to a text string.

Yisrael

Thanks Yisreal! I actually found where the text string was missing in the code on Wix’s support page (https://support.wix.com/en/article/creating-an-expandable-text-box-with-a-show-more-link-using-wix-code) and added it. This is the code Wix tells people to add:

  1. // how many characters to include in the shortened version
    2. const shortTextLength = 40;
    3. // read the full text and store it in the fullText variable
    4. fullText = $w(“#myTextElement”).text;
    5. // // grab the number of characters defined in shortTextLength and store them in the shortText variable 6. shortText = fullText.substr(0, shortTextLength) + “…”;
    7. // set the contents of the text element to be the short text
    8. $w(“#myTextElement”).text = shortText;

As you can see on Line 8, it’s missing the string. It should be $w(“#myTextElement”).text === shortText;

The new problem, if anyone can help, is that - again per the instructions Wix has on the “Creating An Expandable Text Box…” - I have set the button to toggle (Show More, Show Less). The problem is that when the page opens, the Text Box is expanded when (according to the support page) it should be collapsed, with the toggled button to show the full text (Show More).

I can’t seem to make the fullText hide when the page first opens and gives the viewer the option to Show More.

This is the exact code Wix directs you to put in (for everything including the Text box, save for the correction I made above)

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 = 60;
// read the full text and store it in the fullText variable
fullText = $w(“#text46”).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(“#text46”).text === shortTextLength;
});

export function button8_click(event, $w) {
// if currently displaying short text, display the full text
if ($w(“#text46”).text === shortText) {
$w(“#text46”).text = fullText;
$w(“#button8”).label = “Show less”;
} else {
// if currently displaying full text, display the short text
$w(“#text46”).text = shortText;
$w(“#button8”).label = “Show more”;
}

}

HELLO i have 10 working expandable texts on my website using these codes:

let fullText10; // variable to hold the full text
let shortText10; // variable to hold the short version of the text

// For full API documentation, including code examples, visit Velo API Reference - Wix.com

$w.onReady( function () {
// how many characters to include in the shortened version
const shortText10Length = 0;
// read the full text and store it in the fullText variable
fullText10 = $w(“#text122”).html;
// grab the number of characters defined in shortTextLength and store them in the shortText variable
shortText10 = fullText10.substr(0, shortText10Length) + “”;
// set the contents of the text element to be the short text
$w(“#text122”).html = shortText10;
});
export function button14_click() {
// check the contents of the text element
if ($w(“#text122”).html.length < fullText10.length) {
// if currently displaying short text, display the full text
$w(“#text122”).html = fullText10;
$w(“#button14”).label = “Show less”;
} else {
// if currently displaying full text, display the short text
$w(“#text122”).html = shortText10;
$w(“#button14”).label = “Show more”;
}
}

FOR YOUR REFERENCE THIS IS MY WEBSITE WITH EXPANDABLE TEXTS: https://www.fridmanproperties.com/airbnb-vs-long-term-rentals

Same issue and mind blown that no Wix or corvid Admin have commented back on this thread…

Thank you Fridman, but in order to make it work, I had to use the procedure from Wix help to add an onClick Event Handler.
See below:

let fullText150; // variable to hold the full text
let shortText150; // variable to hold the short version of the text
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
$w.onReady( function () {
// how many characters to include in the shortened version
const shortText150Length = 150;
// read the full text and store it in the fullText variable
fullText150 = $w(“#text74”).html;
// grab the number of characters defined in shortTextLength and store them in the shortText variable
shortText150 = fullText150.substr(0, shortText150Length) + “”;
// set the contents of the text element to be the short text
$w(“#text74”).html = shortText150;
});
export function button31_click_1(event) {
// check the contents of the text element
if ($w(“#text74”).html.length < fullText150.length) {
// if currently displaying short text, display the full text
$w(“#text74”).html = fullText150;
$w(“#button31”).label = “Show less”;
} else {
// if currently displaying full text, display the short text
$w(“#text74”).html = shortText150;
$w(“#button31”).label = “Show more”;