A common solution is to store your sports / facility pairs as data, as it is data.
var tbl = { tennis: 'raquet', snooker: 'cue', ... };
and then use this simple code:
$('select').on("change", function(){ var t = tbl[$(this).val()]; if (typeof t === "string") $('.sport').val(t); });
You can even get tbl from a separate JSON file (or just save it in another JS file) for easier management.
Suppose you want to use a separate JSON file called things.json , then your file will look like this:
{ "tennis": "raquet", "snooker":"cue", ... }
And the code will be like this:
var httpRequest = new XMLHttpRequest(); httpRequest.onreadystatechange = function() { if (httpRequest.readyState === 4) { if (httpRequest.status === 200) { var tbl = JSON.parse(httpRequest.responseText); $('select').on("change", function(){ var t = tbl[$(this).val()]; if (typeof t === "string") $('.sport').val(t); }); } } }; httpRequest.open('GET', 'things.json?time='+Date.now()); httpRequest.send();
source share