There are no special reasons why this should be more complicated than an array, right?
var strs = [ "This is string 1", // Note, this is strs[0], not strs[1] "This is string 2" ]; function test() { var selectedOption = $("select#myoptions option:selected").attr("id"); $("div#result").html(strs[selectedOption]); } $("select#myoptions").change(test);
If you really need to, you can use a hash like
var strs = { dog: "This is string 1", cat: "This is string 2" };
but in any case, 99% of the time when people try to find one variable using one constant value (i.e. the name of the array) and one variable (i.e. the key), they want to use something simpler, like an array or a hash.
source share