HELP! ADD ON - Change Label on Button matching selected element

Hi so below is a link to my current project:

Here is a Link to my project

Here is my code that makes up my current project. Everything works just need the label to change to what matches with is selected from drop down. Please anyone know how to do this? Here is my code:

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(function () {
    $w("#countyList").options = [
    {"label": "Utah County", "value": "/utah-county"},
    {"label": "Second County", "value": "/second-county"},
    {"label": "Third County", "value": "/Third-County"},
];
 if ($w('#countyList').options === "label" && "Utah County") {
        $w('#button5').label === "Utah County";
    } else if ($w('#countyList').options === "Second County") {
        $w('#button5').label === "Second County";
    } else if ($w('#countyList').options === "label" && "Third County") {
        $w('#button5').label === "Third County";
    } else {
        $w('#button5').disable();
    }
})

$w.onReady(function () {
    $w('#button5').disable();
    $w('#button5').label = "Select a County";
})

export function button5_click(event) {
 let goToUrl = $w('#countyList').value;
    wixLocation.to(goToUrl);
}

export function countyList_change(event) {
    $w('#button5').enable();
}

ADD ON!

One thing. I am trying to add on to this. So basically I want “Select Your County To Get Started” to show in the list of dropdown at the top. So basically if you select that it will disable the button with label saying “Select County” and when selecting county it will enable like how it does already. So basically just adding “Select Your County To Get Started” option which I have already. But it enables the button when click on that option I want it so if you select that option will disable it and select county it will enable then if reselect that first option it will disable button. Here is screen shot below and current code. If need clear information let me know if this is not clear enough.

Step 1: When you first open the drop down option list

Step 2: After opening the drop down list and selecting that option “Select Your County To Get Started”

Step 3: After selecting a county from the list in this case I selected “Box Elder County” and then I went back and selecting the first option “Select Your County To Get Started” and it still kept that label and did not disable the button with changing label back to “Select a County”

click here for example of the current problem I am having.

Here is my code:

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(function () {
    $w('#countyList').placeholder = "Select Your County To Get Started";
    $w('#button5').disable();
    $w('#button5').label = "Select a County";

    $w("#countyList").options = [
    {"label": "Select Your County To Get Started"},
    {"label": "Beaver County", "value": "/beaver-county"},
    {"label": "Box Elder County", "value": "/box-elder-county"},
    {"label": "Cache County", "value": "/cache-county"},
    {"label": "Carbon County", "value": "/carbon-county"},
    {"label": "Daggett County", "value": "/daggett-county"},
    {"label": "Davis County", "value": "/davis-county"},
    {"label": "Duchesne County", "value": "/duchesne-county"},
    {"label": "Emery County", "value": "/emery-county"},
    {"label": "garfield County", "value": "/garfield-county"},
    {"label": "Grand County", "value": "/grand-county"},
    {"label": "Iron County", "value": "/iron-county"},
    {"label": "Juab County", "value": "/juab-county"},
    {"label": "Kane County", "value": "/kane-county"},
    {"label": "Millard County", "value": "/millard-county"},
    {"label": "Morgan County", "value": "/morgan-county"},
    {"label": "Piute County", "value": "/piute-county"},
    {"label": "Rich County", "value": "/rich-county"},
    {"label": "Salt Lake County", "value": "/salt-lake-county"},
    {"label": "San Juan County", "value": "/san-juan-county"},
    {"label": "Sanpete County", "value": "/sanpete-county"},
    {"label": "Sevier County", "value": "/sevier-county"},
    {"label": "Summit County", "value": "/summit-county"},
    {"label": "Tooele County", "value": "/tooele-county"},
    {"label": "Uintah County", "value": "/uintah-county"},
    {"label": "Utah County", "value": "/utah-county"},
    {"label": "Wasatch County", "value": "/wasatch-county"},
    {"label": "Washington County", "value": "/washington-county"},
    {"label": "Wayne County", "value": "/wayne-county"},
    {"label": "Weber County", "value": "/weber-county"}
];
})

 export function countyList_change(event) {
    $w('#button5').enable();
 let options = $w('#countyList').options; // this is the code to make sure options is defined. <<<
 let idx =  $w('#countyList').selectedIndex
    $w('#button5').label = options[idx].label;
}

export function button5_click(event) {
 let goToUrl = $w('#countyList').value;
    wixLocation.to(goToUrl);
}

If you can help me figure this out so basically to shorten it down. The main issue is:

Step 1: click drop down and if select option and reselect the first option again it disables with label “Select County”

Step 2: And once you select another option it enables. Everything is working just the first option should not be a choice it should keep button disabled.

First, you should only have one page onReady() function to ensure reliable and predictable execution of your code.

What label do you want to change? The label to the button? If so, then changing you countryList_change() function to something like this should help:

export function countyList_change(event) {
    let idx =  $w('#countyList').selectedIndex
    $w('#button5').label = options[idx].label;
}

Note: This is untested, but it should give you pretty much what you want.

I added this code to my page and it won’t even open the drop down menu… Anyways why this would not open the drop down list?

I am just getting that options is not defined as shown in the screenshot:

How would I correct this?

The code worked I the reason I was getting “options is not defined” was because I had to set it as a variable so i added this code into it

export function countyList_change(event) {
    $w('#button5').enable();
 let options = $w('#countyList').options; // this is the code to make sure options is defined. <<<
 let idx =  $w('#countyList').selectedIndex
    $w('#button5').label = options[idx].label;
}

And than the code started working on preview mode. And would not work on published site. Or if I first loaded the page it would work and when i go to another page and go back to the page it would not work. And all I did was rearranged my code. So it now works with NO database setup. The final code looks like this:

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(function () {

    $w('#countyList').placeholder = "Select Your Country To Get Started";
    $w('#button5').disable();
    $w('#button5').label = "Select a County";

    $w("#countyList").options = [
    {"label": "Beaver County", "value": "/beaver-county"},
    {"label": "Box Elder County", "value": "/box-elder-county"},
    {"label": "Cache County", "value": "/cach-county"},
    {"label": "Carbon County", "value": "/carbon-county"},
    {"label": "Daggitt County", "value": "/daggitt-county"},
    {"label": "Davis County", "value": "/davis-county"},
    {"label": "Duchesne County", "value": "/duchesne-county"},
    {"label": "Emery County", "value": "/emery-county"},
    {"label": "garfield County", "value": "/garfield-county"},
    {"label": "Grand County", "value": "/grand-county"},
    {"label": "Iron County", "value": "/iron-county"},
    {"label": "Juab County", "value": "/juab-county"},
    {"label": "Kane County", "value": "/kane-county"},
    {"label": "Millard County", "value": "/millard-county"},
    {"label": "Morgan County", "value": "/morgan-county"},
    {"label": "Piute County", "value": "/piute-county"},
    {"label": "Rich County", "value": "/rich-county"},
    {"label": "Salt Lake County", "value": "/salt-lake-county"},
    {"label": "San Juan County", "value": "/san-juan-county"},
    {"label": "Sanpete County", "value": "/sanpete-county"},
    {"label": "Sevier County", "value": "/sevier-county"},
    {"label": "Summit County", "value": "/summit-county"},
    {"label": "Tooele County", "value": "/tooele-county"},
    {"label": "Uintah County", "value": "/uintah-county"},
    {"label": "Utah County", "value": "/utah-county"},
    {"label": "Wasatch County", "value": "/wasatch-county"},
    {"label": "Washington County", "value": "/washington-county"},
    {"label": "Wayne County", "value": "/wayne-county"},
    {"label": "Weber County", "value": "/weber-county"}
];
})

 export function countyList_change(event) {
    $w('#button5').enable();
 let options = $w('#countyList').options; // this is the code to make sure options is defined. <<<
 let idx =  $w('#countyList').selectedIndex
    $w('#button5').label = options[idx].label;
}


export function button5_click(event) {
 let goToUrl = $w('#countyList').value;
    wixLocation.to(goToUrl);
}

@searleaustin91 Hah! Sorry, I told you it was “untested”. Glad you were able to figure it out in spite of untested code.

@yisrael-wix Hey Yisrael, one thing. I am trying to add on to this. So basically I want “Select Your County To Get Started” to show in the list of dropdown at the top. So basically if you select that it will disable the button with label saying “Select County” and when selecting county it will enable like how it does already. So basically just adding “Select Your County To Get Started” option which I have already. But it enables the button when click on that option I want it so if you select that option will disable it and select county it will enable then if reselect that first option it will disable button. Here is screen shot below and current code. If need clear information let me know if this is not clear enough.

Step 1: When you first open the drop down option list

Step 2: After opening the drop down list and selecting that option “Select Your County To Get Started”

Step 3: After selecting a county from the list in this case I selected " Box Elder County " and then I went back and selecting the first option " Select Your County To Get Started " and it still kept that label and did not disable the button with changing label back to " Select a County "

click here for example of the current problem I am having.

Here is my code:

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(function () {
    $w('#countyList').placeholder = "Select Your County To Get Started";
    $w('#button5').disable();
    $w('#button5').label = "Select a County";

    $w("#countyList").options = [
    {"label": "Select Your County To Get Started"},
    {"label": "Beaver County", "value": "/beaver-county"},
    {"label": "Box Elder County", "value": "/box-elder-county"},
    {"label": "Cache County", "value": "/cache-county"},
    {"label": "Carbon County", "value": "/carbon-county"},
    {"label": "Daggett County", "value": "/daggett-county"},
    {"label": "Davis County", "value": "/davis-county"},
    {"label": "Duchesne County", "value": "/duchesne-county"},
    {"label": "Emery County", "value": "/emery-county"},
    {"label": "garfield County", "value": "/garfield-county"},
    {"label": "Grand County", "value": "/grand-county"},
    {"label": "Iron County", "value": "/iron-county"},
    {"label": "Juab County", "value": "/juab-county"},
    {"label": "Kane County", "value": "/kane-county"},
    {"label": "Millard County", "value": "/millard-county"},
    {"label": "Morgan County", "value": "/morgan-county"},
    {"label": "Piute County", "value": "/piute-county"},
    {"label": "Rich County", "value": "/rich-county"},
    {"label": "Salt Lake County", "value": "/salt-lake-county"},
    {"label": "San Juan County", "value": "/san-juan-county"},
    {"label": "Sanpete County", "value": "/sanpete-county"},
    {"label": "Sevier County", "value": "/sevier-county"},
    {"label": "Summit County", "value": "/summit-county"},
    {"label": "Tooele County", "value": "/tooele-county"},
    {"label": "Uintah County", "value": "/uintah-county"},
    {"label": "Utah County", "value": "/utah-county"},
    {"label": "Wasatch County", "value": "/wasatch-county"},
    {"label": "Washington County", "value": "/washington-county"},
    {"label": "Wayne County", "value": "/wayne-county"},
    {"label": "Weber County", "value": "/weber-county"}
];
})

 export function countyList_change(event) {
    $w('#button5').enable();
 let options = $w('#countyList').options; // this is the code to make sure options is defined. <<<
 let idx =  $w('#countyList').selectedIndex
    $w('#button5').label = options[idx].label;
}


export function button5_click(event) {
 let goToUrl = $w('#countyList').value;
    wixLocation.to(goToUrl);
}

If you can help me figure this out so basically to shorten it down. The main issue is:

Step 1: click drop down and if select option and reselect the first option again it disables with label “Select County”

Step 2: And once you select another option it enables. Everything is working just the first option should not be a choice it should keep button disabled.

@searleaustin91 Your issues result from the fact that you are treating the first option as “just another choice”. You should be treating the first option differently, sort of like this:

Give the first option a value:

        { "label": "Select Your County To Get Started", "value": "none" },

Then change the countyList_change() function:

export function countyList_change(event) {
   let options = $w('#countyList').options; // this is the code to make sure options is defined. <<<
   let idx = $w('#countyList').selectedIndex;
   if (idx === 0) { // no option selected
      $w('#button5').label = "Select a County";
      $w('#button5').disable();
   } else {
      $w('#button5').label = options[idx].label;
      $w('#button5').enable();
   }
}

Disclaimer: This is for illustration only. It might work, it might not, it should.

I hope this helps. It should come close.

@yisrael-wix Thanks, that did work. I do like how if you select the first option “Select Your County To Get Started” and it has error like a red outline or message letting the user know that they must select a option to get started. How would this be done?

@yisrael-wix So basically, Have red outline around the dropdown if the first option is selected “Select Your County To Get Started”; if that is selected to show red outline around the dropdown option with a error text message saying what they need to select like “Please select a option from the list to get started” error message. How can I do this?

@yisrael-wix If you can help me with this and I would like for the button to load like how corvid wix button loads when publishing a comment or clicking on button. And the above request with error.

@searleaustin91 I don’t understand what you are trying to accomplish. Seems like this is a different issue from your original question.

Please post a new question explaining what you are trying to do, what you have so far, what works, and what doesn’t. Show the relevant code in a code block.

@yisrael-wix Okay, so here is what I am trying to accomplish. I have everything working. What I am trying to add on is as follows.

Issue 1: Add error message with text indicating that they need to make a selection of their county to get started. So the error text telling them what to do and with red border indicating that is the the selection they need to select. (I will have screenshots below that I will edit to how I want it to look like)

Issue 2: When they make their selection they can click the button and have it load with 3 dots animated like how wix code has it set in the forum discussion when publishing or post comments.

Those are the two things to finish my work. Here is my code:

import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady(function () {
    $w('#countyList').placeholder = "Select Your County To Get Started";
    $w('#button5').disable();
    $w('#button5').label = "Select a County";

    $w("#countyList").options = [
    { "label": "Select Your County To Get Started", "value": "none" },
    {"label": "Beaver County", "value": "/beaver-county"},
    {"label": "Box Elder County", "value": "/box-elder-county"},
    {"label": "Cache County", "value": "/cache-county"},
    {"label": "Carbon County", "value": "/carbon-county"},
    {"label": "Daggett County", "value": "/daggett-county"},
    {"label": "Davis County", "value": "/davis-county"},
    {"label": "Duchesne County", "value": "/duchesne-county"},
    {"label": "Emery County", "value": "/emery-county"},
    {"label": "garfield County", "value": "/garfield-county"},
    {"label": "Grand County", "value": "/grand-county"},
    {"label": "Iron County", "value": "/iron-county"},
    {"label": "Juab County", "value": "/juab-county"},
    {"label": "Kane County", "value": "/kane-county"},
    {"label": "Millard County", "value": "/millard-county"},
    {"label": "Morgan County", "value": "/morgan-county"},
    {"label": "Piute County", "value": "/piute-county"},
    {"label": "Rich County", "value": "/rich-county"},
    {"label": "Salt Lake County", "value": "/salt-lake-county"},
    {"label": "San Juan County", "value": "/san-juan-county"},
    {"label": "Sanpete County", "value": "/sanpete-county"},
    {"label": "Sevier County", "value": "/sevier-county"},
    {"label": "Summit County", "value": "/summit-county"},
    {"label": "Tooele County", "value": "/tooele-county"},
    {"label": "Uintah County", "value": "/uintah-county"},
    {"label": "Utah County", "value": "/utah-county"},
    {"label": "Wasatch County", "value": "/wasatch-county"},
    {"label": "Washington County", "value": "/washington-county"},
    {"label": "Wayne County", "value": "/wayne-county"},
    {"label": "Weber County", "value": "/weber-county"}
];
})

 export function countyList_change(event) {
 let options = $w('#countyList').options; // this is the code to make sure options is defined. <<<
 let idx = $w('#countyList').selectedIndex;
 if (idx === 0) { // no option selected
      $w('#button5').label = "Select a County";
      $w('#button5').disable();
   } else {
      $w('#button5').label = options[idx].label;
      $w('#button5').enable();
   }
}


export function button5_click(event) {
 let goToUrl = $w('#countyList').value;
    wixLocation.to(goToUrl);
}

Here is a screenshot of error I am trying to accomplish: (Note: this is not applied in the code above I sorta made it look like an error for this example).

As you can see with screenshot below. There is an error message because the option “Select Your County To Get Started” was selected instead of a option in the list. As well as a message indicating the error which is just a text. I want this error to show only when a option is not selected on the list with button disabled.

And this next one is when they select a option and then no error shows and they click the button and it does 3 dot animation indicating that its loading and takes them to that desired web page (which already is coded and working perfectly.)

This animation that will show on the button replacing the text with loading dots not stars. (Sorry could not find one with dots) But same effect just no stars.

Or even this one with saying Loading and the county they selected with a loading like this under the text on the button.

Hope this provides what exactly I am trying to accomplish. Sorry for the confusion. Hopefully, this is more clear.

@searleaustin91 For a validation error message, see then Custom Validations example.

The Preloader for File Upload demonstrates how to implement a preloader. You can easily adapt the code to show a page loader.