Show more button code

Hi. I followed the corvid tutorial to create a show more show less button to a text box in a repeater tied to a database. No error messages are showing, but when I preview, the text box and button are missing from the page. Here in my code. Any ideas what is wrong?

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

$w.onReady( function () {
$w(“#dataset1”).onReady( function () {
const shortTextLength = 50;
fullText = $w(‘#dataset1’).getCurrentItem().textField;
if (!fullText) {
$w(‘#text26’).collapse();
$w(‘#button3’).collapse();
} else {
if (fullText.length <= shortTextLength) {
$w(‘#text26’).text = fullText;
$w(‘#button3’).collapse();
} else {
shortText = fullText.substr(0, shortTextLength) + “…”;
$w(‘#text26’).text = shortText;
}
}
});
});

export function button3_click(event) {
if ($w(“#text26”).text === shortText) {
$w(“#text26”).text = fullText;
$w(“#button3”).label = “Show less”;
} else {
$w(“#text26”).text = shortText;
$w(“#button3”).label = “Show more”;
}
}

Why are you collapsing the text and the button?

it looks like you don’t have a field key in your collection called textField

I am following the tutorial code exactly. I want long stories to be shortened so a minimal, say 50, words are showing. That way, we can show more articles on the screen. The viewer should be able to click the show more button to get the full text. Or click the show less button when in full text to get less. I assume that collapse means, collapse to 50 words.

@jonatandor35 #text26 identifies the text field according to Corvid

@lanewriters I’m not sure what you’re saying.
In this line:

    fullText = $w('#dataset1').getCurrentItem().textField; 

You define the fullText as the value of the textField column in your database collection. But apparently you just didn’t create such a column.

@jonatandor35 I can’t vouch for what I’m saying either. I’m not a coder, so the language is new to me. I hear you saying that Corvid’s tutorial is lacking an element, correct? My text fills from a data set. That dataset is named in the line you pasted in. Corvid’s tutorial is different for text that is pasted into a text field. The text field fills nicely without the show more button code, so I know that link is working. Since I’m not a coder, can you explain more fully? Are you saying that textField needs more definition? Of course, my dataset has multiple text fields. Is that the issue? But the #text26 is hooked to the correct text field in the repeater.

@jonatandor35 Ah. You think I didn’t create the correct column in the dataset. But, I did and it fills fine without this button code.

@lanewriters

So you are using this tutorial
https://support.wix.com/en/article/corvid-tutorial-creating-a-show-more-link

Using the code for the dynamic text option instead of the static text option.

This is the full code from the tutorial for the show more/show less button option…

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

$w.onReady(function () {
$w("#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').getCurrentItem().textField;
// if no text to display, collapse the text element and the button
if (!fullText) {
$w('#myTextElement').collapse();
$w('#myButton').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('#myTextElement').text = fullText;
$w('#myButton').collapse();
} else {
// create the shortened version of the text and display it in the text element
shortText = fullText.substr(0, shortTextLength) + "...";
$w('#myTextElement').text = shortText;
}
}
});

export function mybutton_click(event, $w) { //make sure you have added the onClick event in properties
// check the contents of the text element 
if ($w("#myTextElement").text === shortText) {
// if currently displaying short text, display the full text
$w("#myTextElement").text = fullText;
$w("#myButton").label = "Show less"; 
} else {
// if currently displaying full text, display the short text
$w("#myTextElement").text = shortText;
$w("#myButton").label = "Show more";
}
}

So, when you mention in your post that…
but when I preview, the text box and button are missing from the page.

Well look at these few lines of code from within your code used.

 // if no text to display, collapse the text element and the button
 if (!fullText) {
 $w('#myTextElement').collapse();
 $w('#myButton').collapse(); 

So, one important question for you…
Have you got any text setup in your text field in your dataset?

Remembering that the code itself needs to be calling the text from the text field in your dataset and not what field you have linked the element to on your page.

If not, then the code will read it as nothing and collapse the box and button, hence the no showing when you preview.

From the page
This time, the code first reads the contents of the text field of the current item being displayed and stores it in the fullText variable. To make sure that we cover all possibilities, the code then runs 2 checks:

  1. It makes sure the field in question actually has text to display. If it does not, both the text element and the button are collapsed. This means your visitors won’t see a “Show More” button without any text above it.

  2. It checks the length of the text to make sure it’s longer than the value defined in the shortTextLength variable. If it is not, the text element displays the full text, and the button is collapsed. This means that your visitors won’t see the “Show More” button because there is no more text to show.

Line 8 calls the getCurrentItem() function to read the contents of the textField of the current item and then store it in the fullText variable.

Lines 10-12 check to see if the fullText variable has a value. If the field in the collection is empty, that field is not returned by getCurentItem(), so fullText will have no value. In this case, both the text element and the button are collapsed because there is nothing to display.

@givemeawhisky Sorry, you’ve lost me. What do you mean by " text setup in your text field in your dataset?" Specificially, the text setup. Yes, there is text in that field currently. What specific text setup do you mean?

@lanewriters

With your #text26 text box element, have you connected it to your dataset through the element itself like in this tutorial here.
https://support.wix.com/en/article/displaying-database-content-on-a-regular-page#6-connect-your-page-elements-1

If you have, then you need to undo and delete that connection as that is overwriting the code that you are using.

The line in your code itself is used to get the text from your required text field in your dataset, so you simply need to change textField to whatever field in your dataset you are using.

Currently, it seems to me that you have it linked to your dataset field via the text box element itself, so that when you run the page on preview, the code does not see any text for that field and hence reads it as null and collapses both the text box and button.

Also, why the text box displays the text without the show more/show less toggle code, because it is reading the text from the dataset field via the dataset link from the element itself.

@givemeawhisky The text field (#text26) is connected to the dataset in that way, yes. But the button tutorial told me to hover over the text box to get it’s name and put that in the code. It didn’t say to go to the dataset. I’m not doubting you, but am confused. How would I find the name of that dataset field appropriate to put in the code? Are you saying to unlink the text box and leave the name (#text26) the same? How will it know which column to look in?

When the tutorial says to hover over the element, it is talking about getting the id name so that you can use that in the code itself. You can also do it by right clicking the element and viewing the properties panel for it.

However, the tutorial itself does not tell you to link the text element to your dataset through the dataset link setting.

You either type in the full text if you are working with the static text option, or you use the dynamic text option and call the text from the field in your dataset.

As for the dataset field, open up your dataset and the fields will be the titles of each of the columns in the dataset itself.

So the first one which is always added by default in a new dataset is Title which has a field key of title, whilst the rest will be whatever you have added as custom fields.

You need to use the field name of the dataset field that contains the full text that will go into your text box element with the id name of #text26.

See here for more info about dataset field names etc.
https://support.wix.com/en/article/about-your-database-collection-fields#regular-fields

Ok. I’m making progress. I now see that a substitution I didn’t make was for the “textField.” But, what I don’t see is where Corvid wants me to make that substitution. The line below this substitution is about the text box on the page. Which line in the code do I put that textField on?

Here is my code now. I looked in preview with the text box connected to the dataset – no good. Then, disconnected it. Still, no good. That dataset column is called “Content.”

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

$w.onReady( function () {
$w(“#dataset1”).onReady( function () {
const shortTextLength = 50;
fullText = $w(‘#dataset1’).getCurrentItem().Content;
if (!fullText) {
$w(‘#text26’).collapse();
$w(‘#button3’).collapse();
} else {
if (fullText.length <= shortTextLength) {
$w(‘#text26’).text = fullText;
$w(‘#button3’).collapse();
} else {
shortText = fullText.substr(0, shortTextLength) + “…”;
$w(‘#text26’).text = shortText;
}
}
});
});

export function button3_click(event) {
if ($w(“#text26”).text === shortText) {
$w(“#text26”).text = fullText;
$w(“#button3”).label = “Show less”;
} else {
$w(“#text26”).text = shortText;
$w(“#button3”).label = “Show more”;
}
}

Ah. It wants the field key, not the field name. The field key is “content.” When I changed it to that, the button now shows in Preview, but the actual content is just two lines with

<p style="... This is a rich text column on the dataset. Does that matter?

Remember that you choose fifty (50) for the short text display limit, so it is basically just doing that with the ellipsis on the end of it.

From tutorial…
Preview your page now to see how only the first 40 characters of your text appear, with an ellipsis at the end. Don’t try clicking the button, because you haven’t written its code yet.

Choosing a text field or a rich text field will depend on what you want to do with the text.
See here for more details.
https://support.wix.com/en/article/connecting-text-elements
https://www.wix.com/corvid/reference/$w.Text.html

Finally, if it is still showing all of the text from the dataset field, even though you have applied your own settings for the rich text, then I would simply try just changing the field type to text as it indicates that the code is reading the full text from the dataset field regardless of the rich text formatting that is applied within it too.

@givemeawhisky Let’s start at your first point. Yes, I chose 50 characters actually. It is showing me none of my characters. Why?

@lanewriters

Yes it is, it is counting the first fifty (50) characters in your dataset text field and showing that with the ellipsis at the end of it.

<p><span class="wixGuard">​</span></p> // 37 characters  
<p style="... // 13 characters

Like I’ve said, it seems that the code takes the full text from whatever text is in the dataset text field completely, so you are better off changing the field type to text and not rich text as it is taking the rich text settings as being part of your text itself.

Also, when you apply the settings via the rich text field, have you saved it and synced the dataset from sandbox to live and published your website and then tested it again?


Is it now giving you the text as you set it all up or is it still giving you the text from within your rich text field with rich text settings shown as well.
As like here - https://www.wix.com/corvid/reference/$w.Text.html