Multiple Expandable Text Boxes : Error

I have followed the wix code forum guide to employ “expandable text” boxes on my site. Although the code works perfectly with one expandable text box - it seems to fail when another is added to the page.

I’ve added the code below. Basically, box #1 has the correct short-text (first 30 characters pulled from it’s corresponding largetext box). However, when I click the “show more” button, it then populates it with the short text from button 2, and then the long/full text from button 2.

If I was to add a third button, this would happen with both the first and second button… (they would look correct upon the page loading, but once the button is clicked - would populate from the third text box).

Any ideas on how this can be resolved? Much appreciated!

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 = 30;
// read the full text and store it in the fullText variable
fullText = $w(“#text17”).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 the text element to be the short text
$w(“#text17”).text = shortText;

});

export function button6_click(event, $w) {
// check the contents of the text element
if ($w(“#text17”).text === shortText) {
// if currently displaying short text, display the full text
$w(“#text17”).text = fullText;
$w(“#button6”).label = “Show Less”;
} else {
// if currently displaying full text, display the short text
$w(“#text17”).text = shortText;
$w(“#button6”).label = “Show More”;
}
}

$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 the text element to be the short text
$w(“#text22”).text = shortText;

});

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

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

Although the code works perfectly with one expandable text box - it seems to fail when another is added to the page.

It fails because the same variables fullText and shortText are used for different expandable text boxes. Create a set of the variables for every expandable text box:

let fullText1; // variable to hold the full text
let shortText1; // variable to hold the short version of the text

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

});

export function button6_click(event, $w) {
 // check the contents of the text element 
 if ($w("#text17").text === shortText1) {
 // if currently displaying short text, display the full text
        $w("#text17").text = fullText1;
        $w("#button6").label = "Show Less";
    } else {
 // if currently displaying full text, display the short text
        $w("#text17").text = shortText1;
        $w("#button6").label = "Show More";
    }
}

let fullText2; // variable to hold the full text
let shortText2; // 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 fullText2 variable
    fullText2 = $w("#text22").text;
 // grab the number of characters defined in shortTextLength and store them in the shortText2 variable
    shortText2 = fullText2.substr(0, shortTextLength) + "...";
 // set the contents of the text element to be the short text
    $w("#text22").text = shortText2;

});

export function button7_click(event, $w) {
 if ($w("#text22").text === shortText2) {
 // if currently displaying short text, display the full text
        $w("#text22").text = fullText2;
        $w("#button7").label = "Show less";
    } else {
 // if currently displaying full text, display the short text
        $w("#text22").text = shortText2;
        $w("#button7").label = "Show more";
    }
}

Thank you - this really helped me! I can’t seem to get my background box and ‘Show More’ buttons to shrink with the text, it only works for the first expandable box… Help please!

https://www.wix.com/corvid/forum/community-discussion/adding-several-expandable-text-boxes-with-read-more-links
https://www.wix.com/corvid/forum/community-discussion/creating-multiple-show-more-show-less-links
https://www.wix.com/corvid/forum/community-discussion/large-annoying-space-after-using-show-more-show-less-w-wix-code

Thank you for those - I’ve worked through each of those threads but still don’t seem to have got anywhere… I’ll copy my code into here and see if I’m missing something!

I have three text boxes: 5, 3, 1
and three buttons: 8, 9 ,10

let fullText1; // variable to hold the full text
let shortText1; // variable to hold the short version of the text

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

export function button8_click(event) {
// check the contents of the text element 
if ($w("#text5").text === shortText1) {
// if currently displaying short text, display the full text
$w("#text5").text = fullText1;
$w("#button8").label = "Show Less"; 
} else {
// if currently displaying full text, display the short text
$w("#text5").text = shortText1;
$w("#button8").label = "Show More";
}
}

let fullText2; // variable to hold the full text
let shortText2; // variable to hold the short version of the text

$w.onReady(function () {
// how many characters to include in the shortened version
const shortText2Length = 354;
// read the full text and store it in the fullText2 variable
fullText2 = $w("#text3").text;
// grab the number of characters defined in shortText2Length and store them in the shortText2 variable
shortText2 = fullText2.substr(0, shortText2Length) + "...";
// set the contents of the text element to be the short text
$w("#text3").text = shortText2;
});

export function button9_click_1(event) {
// check the contents of the text element 
if ($w("#text3").text === shortText2) {
// if currently displaying short text, display the full text
$w("#text3").text = fullText2;
$w("#button9").label = "Show Less"; 
} else {
// if currently displaying full text, display the short text
$w("#text3").text = shortText2;
$w("#button9").label = "Show More";
}
}

let fullText3; // variable to hold the full text
let shortText3; // variable to hold the short version of the text

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

export function button10_click(event) {
// check the contents of the text element 
if ($w("#text1").text === shortText3) {
// if currently displaying short text, display the full text
$w("#text1").text = fullText3;
$w("#button10").label = "Show Less"; 
} else {
// if currently displaying full text, display the short text
$w("#text1").text = shortText3;
$w("#button10").label = "Show More";
}
}

I’m sorry to ask you such a similar question but I really can’t work out what’s happening… The ‘Show More’ button and white background to my text boxes just don’t shrink/expand - except for text5 and button8!

You need to be combining them all up as in the previous posts that were given to you in the links…or

  • All your let fullText, let shortText at the top;

  • Followed by the one onReady function for your page;

  • Then all your const shortText sections;

  • Then all your export function sections.

https://www.wix.com/corvid/forum/community-discussion/adding-several-expandable-text-boxes-with-read-more-links

However, when you want to add multiple versions of the textboxes then your code starts to elongate and ends up looking something like this:

let fullTextOne = ""; // variable to hold the full text
let shortTextOne = ""; // variable to hold the short version of the text
let fullTextTwo = ""; // variable to hold the full text
let shortTextTwo = ""; // variable to hold the short version of the text

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

// how many characters to include in the shortened version
const shortTextTwoLength = 40;
// read the full text and store it in the fullTextTwo variable
fullTextTwo = $w("#text2").text;
// grab the number of characters defined in shortTextTwoLength and store them in the shortTextTwo variable
shortTextTwo = fullTextTwo.substr(0, shortTextTwoLength) + "...";
// set the contents of the text element to be the short text
$w("#text2").text = shortTextTwo;
});

export function button1_click(event) {
//make sure that you add the onClick event in properties 
// check the contents of the text element 
if ($w("#text1").text === shortTextOne) {
// if currently displaying short text, display the full text
$w("#text1").text = fullTextOne;
$w("#button1").label = "Show Less";  
} else {
// if currently displaying full text, display the short text
$w("#text1").text = shortTextOne;
$w("#button1").label = "Show More";
}

export function button2_click(event) {
//make sure that you add the onClick event in properties 
// check the contents of the text element 
if ($w("#text2").text === shortTextTwo) {
// if currently displaying short text, display the full text
$w("#text2").text = fullTextTwo;
$w("#button2").label = "Show Less";
} else {
// if currently displaying full text, display the short text
$w("#text2").text = shortTextTwo;
$w("#button2").label = "Show More";
}
}

Nope - didn’t work. Neater code but didn’t work :frowning:

I did exactly what all those pages said:

let fullTextOne = ""; // variable to hold the full text
let shortTextOne = ""; // variable to hold the short version of the text
let fullTextTwo = ""; // variable to hold the full text
let shortTextTwo = ""; // variable to hold the short version of the text
let fullTextThree = ""; // variable to hold the full text
let shortTextThree = ""; // variable to hold the short version of the text

$w.onReady(function () {
// how many characters to include in the shortened version
const shortTextOneLength = 454;
// read the full text and store it in the fullTextOne variable
fullTextOne = $w("#text5").text;
// grab the number of characters defined in shortTextOneLength and store them in the shortTextOne variable
shortTextOne = fullTextOne.substr(0, shortTextOneLength) + "...";
// set the contents of the text element to be the short text
$w("#text5").text = shortTextOne;

// how many characters to include in the shortened version
const shortTextTwoLength = 354;
// read the full text and store it in the fullTextTwo variable
fullTextTwo = $w("#text3").text;
// grab the number of characters defined in shortTextTwoLength and store them in the shortTextTwo variable
shortTextTwo = fullTextTwo.substr(0, shortTextTwoLength) + "...";
// set the contents of the text element to be the short text
$w("#text3").text = shortTextTwo;

// how many characters to include in the shortened version
const shortTextThreeLength = 484;
// read the full text and store it in the fullTextThree variable
fullTextThree = $w("#text1").text;
// grab the number of characters defined in shortTextThreeLength and store them in the shortTextThree variable
shortTextThree = fullTextThree.substr(0, shortTextThreeLength) + "...";
// set the contents of the text element to be the short text
$w("#text1").text = shortTextThree;
});

export function button8_click(event) {
//make sure that you add the onClick event in properties 
// check the contents of the text element 
if ($w("#text5").text === shortTextOne) {
// if currently displaying short text, display the full text
$w("#text5").text = fullTextOne;
$w("#button8").label = "Show Less";  
} else {
// if currently displaying full text, display the short text
$w("#text5").text = shortTextOne;
$w("#button8").label = "Show More";
}
}

export function button9_click_1(event) {
//make sure that you add the onClick event in properties 
// check the contents of the text element 
if ($w("#text3").text === shortTextTwo) {
// if currently displaying short text, display the full text
$w("#text3").text = fullTextTwo;
$w("#button9").label = "Show Less";
} else {
// if currently displaying full text, display the short text
$w("#text3").text = shortTextTwo;
$w("#button9").label = "Show More";
}
}

export function button10_click(event) {
//make sure that you add the onClick event in properties 
// check the contents of the text element 
if ($w("#text1").text === shortTextThree) {
// if currently displaying short text, display the full text
$w("#text1").text = fullTextThree;
$w("#button10").label = "Show Less";
} else {
// if currently displaying full text, display the short text
$w("#text1").text = shortTextThree;
$w("#button10").label = "Show More";
}
}

And still only the first box’s background and button collapse with the text. :((((

Did you ever get this figured out? I’m trying to do multiple expanding text boxes for an FAQ page.

Nope. No help was found or given xD I just reformatted the site so that you click a link and it opens up into a new page instead… ¯_(ツ)_/¯ Sorry - it’s really not ideal. I’d love to know how to fix that issue if you ever find out!

I just found a much simpler way to do this, requiring significantly less code, no white space, and the ability to keep formatting without using html.

It involves 2 text boxes per Show More/Less, instead of just 1. The 1st text box contains your text that is always shown, the 2nd text box contains the text you want to show and hide.
Select the Collapsed checkbox in the properties dialogue for text2, and the onClick() property for your button.
Type the desired text, whatever you’d like in both text boxes. As for the code, this is it in its entirety:

$w.onReady(function () {
});

export function button1_click(event) {
 // check if text2 is hidden
 if ($w("#text2").collapsed) {
 // show text2 and change button label
        $w('#text2').expand();
        $w("#button1").label = "Show Less";
    } else {
 // hide text2 and change button label
        $w('#text2').collapse();
        $w("#button1").label = "Show More";
    }
}

Thats it. To add additional dropdowns, simply repeat the process and update the required ID fields (text2 to text4, button1 to button2 perhaps)

Hope this helps!