2

I have a brand list with about 2000 items, my problem is I want to generate a list of commands In jquery using this format dynamically.

 $("select[name='brand']").change(function () {
     $("#brand1,#brand2").hide();
     if ($(this).val() == "brand1") { $("#brand1").show(); } 
     else if ($(this).val() == "brand2") { $("#brand2").show(); }
       and so on...
 });

the list of brands is located in MySQL which I brought into an array called

allBrands[] in php

so if the brands update in the MySQL, it will also update in the jquery script.

Obviously I can manually type in each brand, but i'm worried about when I update the database for newer brands etc..

Edit: that being said, if I can do a MySQL call in jquery and get the list of brands that way, that would also work. Brand1, brand2 = examples, names are random based on brand

1 Answer 1

1

If the data is ordered in the same way your 2 examples suggest you could try this:

$("select[name='brand']").change(function () {
     $("[id^=brand]").hide();  // all id's starting with the word "brand"
     $("#" + this.value).show(); // if the value is the same as the id you want to target
 });

About the jQuery ^=, read here.

If the brands do not start with the word brand you can use $(".brands").hide();, and use the rest as I posted.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry, I don't know jquery that well, can you explain how your new code works? what does id^ do? the data in MySQL is in a column called brands, the brand1 brand2 is an example.
@AlbertD, just added a link to more info on that selector. Also and dispite my many reservations about w3schools, here is a link with many different selectors: w3schools.com/jquery/jquery_ref_selectors.asp
where do you get this.value from? how do you generate the list in jquery of all the brands, I can't use the first example cause the brands are not numbered they are random names of brands, but thanks for the link to help me understand it..
@AlbertD, this.value is the selected value of the select. So, when the select changed this.value is the new selected value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.