Look at here,
if($(this).text() == "Grocery"){ $(".type_changer").attr("id", "gro"); }else if($(this).text() == "Restaurant"){ $(".type_changer").attr("id", "res"); }else if($(this).text() == "Bar"){ $(".type_changer").attr("id", "bar"); }else if($(this).text() == "Pizza Delivery"){ $(".type_changer").attr("id", "piz"); }else if($(this).text() == "Quick Service"){ $(".type_changer").attr("id", "qui"); }else if($(this).text() == "Retail"){ $(".type_changer").attr("id", "ret"); }else if($(this).text() == "Salon"){ $(".type_changer").attr("id", "sal"); }
You have to think about all reps. What will remain? Right, the text and id values ββare:
"Grocery", "gro" "Restaurant", "res" "Bar", "bar" "Pizza Delivery", "piz" "Quick Service", "qui" "Retail", "ret" "Salon", "sal"
Let them be stored in some data structure. The facility is an obvious choice.
var types = { "Grocery": "gro", "Restaurant": "res", "Bar": "bar", "Pizza Delivery": "piz", "Quick Service": "qui", "Retail": "ret", "Salon": "sal" }
Access to it can be obtained as an associative array with dynamic keys. Now you can use one line:
$(".type_changer").attr("id", types[$(this).text()]);
How to replace the second part remains an exercise for you, but it boils down to the same.
Update : you find it hard to understand . Here is an explanation on my part:
When $(this).text() returns "Grocery" , the above will be resolved
$(".type_changer").attr("id", types["Grocery"]);
types["Grocery"] in turn return "gro" , so it basically ends up like
$(".type_changer").attr("id", "gro");
when $(this).text() is "Grocery" .
See also: