Question:
If a CMS field (link info) is blank, do not display the button (button4) on the webpage.
Product:
Wix Editor in Dev mode
What are you trying to achieve:
Don’t display a button when there is no data in the related CMS field. I have a list of companies in a repeater on my webpage, followed by a button that takes the user to a URL that’s stored in the CMS. But some rows in CMS do not have a URL. When they don’t have a URL, I want to hide the button for that row. It is my understanding a simple JavaScript could handle this situation.
What have you already tried:
My background is a Server admin. It’s many years since I did any coding. I did try this code sample example in a forum post but didn’t work.
$w.onReady(function () {
$w(‘#dataset1’).onReady(() => {
$w(“#repeater1”).onItemReady(($item, itemData) => {
let sTitle = currentItem.urlField;
if (sTitle === “” || sTitle === null || sTitle === undefined) {
$item(“#myButton”).hide();
} else {
$item(“#myButton”).show();
}
});
});
})
Additional information:
See the attachments. 1) of the webpage and 2) of the CMS data. Note the first row in CMS has no link info.
This worked! Thank you @onemoretime for the assist. Now I need to apply similar code to other link buttons and future repeaters. I’ll be back if I have more questions.
When a visitor has a slow internet connection, laggy browser or the page delays loading for whatever reason—— the elements will be visible on the page before they are hidden via code.
If you open up your code panel, bottom right corner will show the element panel after you click on your element on the page. Within that section you will see a checkbox that says “hidden”. Check that box. This make the element hidden before the page loads.
Ok, is forumName the correct fieldkey ID for the column in the database? Does that column have any data in it? Are you testing on the live website or inside of the editor Preview?
Yes, forumName is the Field ID. That’s the field I am keying on to show or hide the button. So when this field is blank, I hide the button. And I show the button when a URL is present. I’m testing in the editor Preview. I did publish the logic shown below and that works also.
The logic below worked properly but you stepped in a said it would cause ‘blinking’ on a slow connection. And I also liked the idea of simpler code.
I checked everything in my CMS content and there are no field ids called lookinfo.
forumName is the field I’m keying off of to display or hide the button.
Knowing that forumName is the field id to use, how would you write (or correct) your simplified code? I assume you are saying the statement ‘if (itemData.forumName)’ is incorrect?
In our last conversation, you questioned whether forumName is the correct field id. It is.
So I’m asking a code question. In your code, do I replace the statement:
let lookinfoUrl = itemData.lookinfo;
with
let lookinfoUrl = itemData.forumName;
My question is related to the code you provided
$w.onReady(function () {
$w(“#dataset1”).onReady(() => {
$w(“#repeater1”).onItemReady(($item, itemData) => {
if (itemData.forumName) {
$item(“#button4”).show();
}
});
});
Honestly, it would be easier if I was looking at your editor or screenshots because that piece of code is written correct. It’s a simple hide and show. The simplest and most popular code to use in Wix.
(It is difficult to troubleshoot code without seeing everything else in the code panel, on the page, in the settings, in the data.)
I have covered as much as I can guess without being visually present.
These are the most common things to look for …. but let me expand on those
Is the fieldkey correct
Is there any code written before or after this code, if yes, is it merged correctly
Are there multiple onReady
Is the correct dataset being referenced
Is the element set to be hidden on load
Is the element attached to the repeater
Is the correct element being referenced
Is hide/show being used vs collapse/show
Is collapse/expand being used vs hide/expand
Does the code work with other elements but not this one
Does the code work with other fields but not this one
Does the code work on other pages but not this one
Are there any excess spaces, dashes or characters in the code underlined in yellow or red