Let's say I have three objects foo
,bar
, and baz
with different properties. When I change a select option with those values, how would I assign the foo
object to contain all the properties of the object, and not just the value.
From my example you'll see that I have it logging foo
, but it is setting it to the value, not as properties of an object.
HTML
<select name="objects" id="objects">
<option value="foo">Foo</option>
<option value="bar">Bar</option>
<option value="baz">Baz</option>
</select>
JavaScript
var foo = {
color: "red",
height: 12
};
var bar = {
color: "blue",
height: 10
};
var baz = {
color: "green",
height: 5
};
$('#objects').on('change', function (e) {
var foo = $(this).val();
console.log($(this).val());
});
Also, is there anyway to trigger this select event, if they select the option that's already selected? So if foo is selected, and they select foo again, trigger the change function.