What I have:
- I have a text box that assumes the value of whatever option is selected in a corresponding select box.
- I'm repeating the exact same function for the on change, mouseup, mousedown, mouseout, keyup and keydown events
What I need:
Can the aforesaid functions be combined into one to produce more efficient code? It just seems terribly repetitious.
My code:
JSFiddle: http://jsfiddle.net/clarusdignus/843YW/1/
HTML:
<label>Industry:</label>
<select name="industry">
<option selected="selected"></option>
<option value="ag">Agriculture</option>
<option value="co">Corporate</option>
</select>
<input type="text" disabled="disabled" name="industryspecifier"/>
jQuery:
$('select[name=industry]').on('change', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});
$('select[name=industry]').on('mouseup', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});
$('select[name=industry]').on('mousedown', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});
$('select[name=industry]').on('mouseout', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});
$('select[name=industry]').on('keydown', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});
$('select[name=industry]').on('keyup', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});