Can the aforesaid functions be combined into one to produce more efficient code? It just seems terribly repetitious.
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());
});
Just combine them with spaces:
$('select[name=industry]').on('change mouseup mousedown mouseout keydown', function() {
$('[name=industryspecifier]').val($(':selected',this).val());
});
jsFiddle example
As the docs for .on() state:
events Type: String One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With