0

I have two dropdowns. When first change event then the second dropdownt loads some data from database.

For example when I select Phones Tablets from the first, then the second one should select Select Brand text. But it loads like below.

My second Dropdown list

<select id="mySelect1">
     <option value="12103">Apple</option>
     <option value="12124">Huawei</option>
     <option value>Select Brand</option>
     <option value="addnew">Enter your own</option>
</select> 

So I use this code for set Selected Band text as selected value in Second dropdown. It uses the change event of my first dropdown list.

            var text1 = 'Select Brand';
            $("select option").filter(function() {
                return $(this).text() == text1; 
            }).prop('selected', true);

My First Dropdown list

<select id="wcv_custom_product_cat" name="wcv_custom_product_cat" class="select2">
     <option value="" selected="selected">Select</option>
     <option value="341">Automotive</option>
     <option value="50">Baby Kids</option>
     <option value="49">Books</option>
     <option value="48">Clothing Fashion</option>
     <option value="47">Computing  Gaming</option>
     <option value="39">Electronics</option>
     <option value="40">Food  Grocery</option>
     <option value="114">Home  Kitchen</option>
     <option value="44">Office Stationary</option>
     <option value="43">Phones  Tablets</option>
     <option value="41">Health  Beauty</option>
     <option value="115">Industrial</option>
     <option value="42">Sports  Fitness</option>
     <option value="45">Misc</option>
</select>

I refer those

jQuery - setting the selected value of a select control via its text description

https://www.aspdotnet-suresh.com/2015/06/jquery-set-dropdownlist-selected-value-text-based-on-text-value.html

How can I change my code?

Rika
  • 333
  • 2
  • 5
  • 19
  • 1
    What is wrong with the answer in https://stackoverflow.com/questions/496052/jquery-setting-the-selected-value-of-a-select-control-via-its-text-description – mplungjan Sep 04 '18 at 07:36
  • @mplungjan I use it's correct answer code.But it's not work for my problem – Rika Sep 04 '18 at 07:40
  • In which case please edit the question to show your attempt to use it, as there will be an error in there we can help you fix. – Rory McCrossan Sep 04 '18 at 07:41
  • @Rory McCrossan Edited.I don't get any error.The thing is logic not work – Rika Sep 04 '18 at 07:45
  • The code you've shown works: http://jsfiddle.net/orpb95je/. I'm not sure what the issue is. – Rory McCrossan Sep 04 '18 at 07:47
  • @RoryMcCrossan I assume he needs `$("#mySelect1 option").` – mplungjan Sep 04 '18 at 07:56
  • So.. it's an error, that when you select Phones Tables you get names of a few companies making Phones and Tablets? If the result was something like Apples AND Tomatoes, then the data would be far enough off the mark to be considered a problem, I think. What is supposed to be returned? – Jan Andersen Sep 04 '18 at 08:00
  • Frist dropdowns has Product, Second one has there Brands – Rika Sep 04 '18 at 08:05

0 Answers0