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
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.