Help with (a rather) simple syntax error

My goal is:
When my dynamicDataset loads, get Current Item and from this get the ‘law’ field. Then check if law’s value is ‘ZZZ’ and if this is true, change the background color of a box.

Hi, this is my code:

$w.onReady(function () {
    $w('#dynamicDataset').onReady(() => {     
 let itemObj = $w("#dynamicDataset").getCurrentItem();
        console.log(itemObj.law)    
 let val = itemObj.law;
        console.log(val)    
 if ($w(val).text === "ZZZ"); {   
        $w("#box23").style.backgroundColor = "rgba(255,0,0,0.5)";     
}     
 //else {         
 //$w('#button2').hide();     
 //} 
});
});

But I get this error in the console:
An error occurred in one of datasetReady callbacks Error: The value passed to the element selector function (usually $w), must be of type string

The ‘law’ field is of type ‘text’. Where am I wrong here please?

The following statement is incorrect:

if ($w(val).text === "ZZZ"); {  

You probably want something like:

if ($w("#" + val).text === "ZZZ"); {  

See this article on template literals.

Hi @yisrael-wix , I don’t need something that fancy. I just want to say: "go read that field’s value. If the value = “A”, then do this. If the value = “B” do that.

@gemats Hmm, not sure what you’re trying to do with this:

if ($w(val).text === "ZZZ"); {   

If the field’s value is val, then something like this:

 if (val === "ZZZ"); {   

or

if(itemObj.law === "ZZZ"); {

What am I missing? What value are you comparing? Comparing to what?

@yisrael-wix Just say that I want to do some conditional formating in a text box.


All these text boxes receive as a value some data from a dataset. So I say: if in that specific text box, the value in the dataset is “A”, then I will paint the background of the text box red to catch my visitor’s attention and will add an onClick event so I’ll have a pop-up giving more information why the value was “A” and what this means for my project purposes.

As a shameful workaround, I inserted a hidden input element, connected to my dataset’s field, in order to make the following work:

if ($w('#input1').value === "Επιτρέπεται") {   
        $w("#box23").style.backgroundColor = "blue";  }     
 else if ($w('#input1').value === 'Δεν επιτρέπεται') {
        $w("#box23").style.backgroundColor = "red"; 

       }

And it works. But it’s not right. I don’t want to add extra fields just because I know how to syntax the query of their values. So instead of saying, compare the input’s value, I just want to learn the syntax (and promise to do some serious study during holidays, something that I always postpone), of: find and compare the value of [myfield] of [mydataset’s] [currentItem]. Very simple.