If database textfield is empty, container box collapse

Hello there! I was wondering if someone could help me with this problem. I have researched and tried a lot of recomendations on the forum, but to no good results.

What I want to do: I Have a dataset that has this [Rich Text field]. Sometimes the field have info, sometimes it has to be empty.
So, into a lightbox I have the text ellement connected to this field, and it goes inside a Container Box. What I need is that the Container Box collapes when the text is empty, but expand when it has info.

I have followed a lot of threads and have found some results, except my current code is collapsing the box even when the dataset field has information and is NOT empty.

Here are my info and current code (I tried it with and without “} else” to the same results:

Collection name: EspecialidadesColection
Dataset name: LBset
Dataset Rich Text field key: altDesc1
Container box to collapse: box000002


$w.onReady(() => {
    $w("#LBset").onReady(() => {   
     const item = $w("#LBset").getCurrentItem();
// Checks if the current item has a value in the "altDesc1" field
        if (!item.altDesc1) {
//to Collapse the box if there is no value in "altDesc1"
            $w("#box000002").collapse();

     } else {
      $w("#box000002").expand();
        }
    });
});

What could I be dooing wrong? I really researched a lot from the forum, but nothing works properly.
Img for reference (the contianer to collapse is the red one):

Code looks OK. Try to console.log the value of item . altDesc1 before the if-statement, so you can see if it has a value at all.

Do this:

// Checks if the current item has a value in the "altDesc1" field
console.log("altDesc1 = " + item.altDesc1);
        if (!item.altDesc1) {

Thank you kindly Giri, I did it like you said. The response log:

Loading the code for the especOPEN page. To debug this code, open au2zp.js in Developer Tools

altDesc1 = undefined

Is that what I should be recieving?

Up to now, I have been trying 5 to 6 versions of the code, and it still collapses in the Countainer Box no matter if the rich text has text or is empty. The “}Else{” command seems to not be any difference.

It means there is no value for altDesc1, thus all collapse. Possible causes: you misspelled altDesc1 (the field name is different), or you used the field Label (in Content manager, the grid, top row) instead of the field key.
LAst possibility: there is nothing to show from the dataset. Good luck.

Thank you again for your help. In case all of these things you said are ok, could it be a bug?
Because I really checked everything you said. As you can see, the field key seems to be correct, there are data in the fields, and there are things to show form the dataset.

Or maybe I misundertood something in this process…

Are you running in Preview Mode or Publish? If latter, have you synced db to “Live”?

I tryed it both ways, published and preview. I also sinced the live. Nothing works. Everything collapses.

Try this code.
Worked fine for me !!!

$w.onReady(function () {
 
    $w('#dataset').onReady(() => {   
 const item = $w('#dataset1').getCurrentItem();

 if (!item.altDesc1) {

            $w('#box2').collapse();

     } else {
      $w('#box2').expand();
        }
    });
});

@ajithkrr Hi, thanks for your answer. I tried it, got the same issue in result , the box that should collapse only if field has no text, is collapsing even when there are text, no matter what. I’m becoming really desperated now, since I have a deadline to deliver the site :persevere:

@lucassmdesign
How about a repeater instead of box ?
https://www.wix.com/corvid/reference/$w/repeater/foreachitem

For repeater you can do this →

$w("#repeater").forEachItem( ($item, itemData, index) => {

    if (itemData.altDesc1 === "") {
     $item('#box').collapse();
     } else {
      $item('#box').expand();
    
  
});

Something like this…

Did it work??

@ajithkrr Tried to use it. It was giving me the following message:


than i tried to add OnRead based on the example at the API, the code got like this



$w.onReady(function () {
  $w("#LBset").onReady( () => {
    $w("#repeaterX").forEachItem(($item, itemData, index) => {

 if (itemData.altDesc1 === "") {
         $item('#box000002').collapse();
        } else {
        $item('#box000002').expand();
 
  }
 })

and it is indicating this error:

@lucassmdesign

$w.onReady(function () {
 $w('#LBset').onReady( () => {
  $w('#repeaterX').forEachItem(($item, itemData, index) => {
   if (itemData.altDesc1 === "") {
         $item('#box000002').collapse();
        } else {
        $item('#box000002').expand();
 
  }
  } );
 } );

} );

Try this !!!

Tried, the error stoped but now its not collapsing at all :joy:

@lucassmdesign
Check if your database fields have any value
Check the repeater id
Check the box id

ALso check if your box is in the repeater