Trying to hide elements based on database

Hello I have container boxes on my web site. I am trying to hide box 1 if in the database the Template value is “1” and then hide the box2
If on the other hand box2 would show if its value is “2” and then hide box1

I tried the code below but it is not working. What am I doing wrong?

$w.onReady(function () {
//TODO: write your page related code here…

$w(“#box1”)( ($w, dynamicDataset, ) => {
console.log(dynamicDataset.template);
if(dynamicDataset.template === “1”) {
$w(“#box1”).show();
}
else { // forSale field is “no”
$w(“#box2”).hide();
}
});

$w("#box2")( ($w, dynamicDataset, ) => { 
console.log(dynamicDataset.template);    
if(dynamicDataset.template === "2") { 
    $w("#box2").show(); 
} 
else {     // forSale field is "no" 
    $w("#box1").hide(); 
} 

});
});

You can read the articles about Connecting Data to learn how to use datasets. The wix-dataset API docs contains all of the details and features of a dataset.

As a quick example, depending on what you are trying to do, your code might look something like this:

$w.onReady( () => {
   $w("#myDataset").onReady( () => {
   let itemObj = $w("#myDataset").getCurrentItem();
   if(itemObj.template === "1") {
      $w("#box1").show();
   } else {
       $w("#box2").hide();
   }
} );

Good luck and have fun,

Yisrael

I did ry to read everything about but having no coding knowledge it is a little bit complicated for me.
I tried applying your code but it is not dong what I want it to do. I’d like the box and everything that is contained inside it to hide. Could it be possible that your code hides the box but not what is inside it?

I believe that hide() hides the box and its contents. The other option is to use collapse() and expand() - which perform similar actions.

We appreciate your interest in really getting the most out of Wix Code and how much you want to learn. Please understand that if you are going to work with code extensively in the product and not just the features in the user interface, you will need to familiarize yourself with basic coding concepts to accomplish what you want. There are a wealth of Javascript coding sites which will help you learn Javascript from basic to advanced - Javascript.info is a good one. The Wix Code Resources page provides tutorials, examples, and articles on getting the most out of Wix Code.

We are happy to get you pointed in the right direction and get you started with what the code should look like, but you’ll need to take it from there. As questions or difficulties arise, we are here to help.

You may also want to check out the WixArena - it’s a hub where you can look for Wix Code (and other) experts for hire.

hello yisrael do this code work the same way for strip
because when i tried to it collapse all strips on my dynamic page
i think something goes wrong with my code

$w.onReady( () => {

$w( "#dynamicDataset" ).onReady( () => {  

let item = $w( “#dynamicDataset” ).getCurrentItem().region;

if(item.region === ‘Bruxelles-Capitale’) {

       $w('#provinceBruxellesCapitale').expand(); 
}  
else { 
  $w('#provinceBrabantFlamand').collapse(); 
  $w('#provinceBrabantWallon').collapse(); 
  $w('#provinceHainautStrip').collapse(); 
  $w('#provinceNamurStrip').collapse(); 
  $w('#provinceLiegeStrip').collapse(); 
} 

if(item.region === 'Brabant Flamand') { 

       $w('#provinceBrabantFlamand').expand(); 
}  
else { 
  $w('#provinceBruxellesCapitale').collapse(); 
  $w('#provinceBrabantWallon').collapse(); 
  $w('#provinceHainautStrip').collapse(); 
  $w('#provinceNamurStrip').collapse(); 
  $w('#provinceLiegeStrip').collapse(); 
} 

if(item.region === 'Brabant wallon') { 

       $w('#provinceBrabantWallon').expand(); 
}  
else { 
  $w('#provinceBruxellesCapitale').collapse(); 
  $w('#provinceBrabantFlamand').collapse(); 
  $w('#provinceHainautStrip').collapse(); 
  $w('#provinceNamurStrip').collapse(); 
  $w('#provinceLiegeStrip').collapse(); 
} 

if(item.region === 'Namur') { 

       $w('#provinceNamurStrip').expand(); 
}  
else { 
  $w('#provinceBruxellesCapitale').collapse(); 
  $w('#provinceBrabantWallon').collapse(); 
  $w('#provinceHainautStrip').collapse(); 
  $w('#provinceBrabantFlamand').collapse(); 
  $w('#provinceLiegeStrip').collapse(); 
} 

if(item.region === 'Liège') { 

       $w('#provinceLiegeStrip').expand(); 
}  
else { 
  $w('#provinceBruxellesCapitale').collapse(); 
  $w('#provinceBrabantWallon').collapse(); 
  $w('#provinceHainautStrip').collapse(); 
  $w('#provinceBrabantFlamand').collapse(); 
  $w('#provinceNamurStrip').collapse(); 
} 

if(item.region === 'Hainaut') { 

       $w('#provinceHainautStrip').expand(); 
}  
else { 
  $w('#provinceBruxellesCapitale').collapse(); 
  $w('#provinceBrabantWallon').collapse(); 
  $w('#provinceLiegeStrip').collapse(); 
  $w('#provinceBrabantFlamand').collapse(); 
  $w('#provinceNamurStrip').collapse(); 
}