Enable if Other is selected

by hansenmc

HTML

<select name="types" multiple="true">
    <option value="1">a</option>
    <option value="2">b</option>
    <option value="3">other</option>
</select>
<input name="otherVal"></input>

JavaScript

$('document').ready(function () {
    var types = $('select').change(function () {
        toggleOther($(this), $('input[name="otherVal"]'));
    });
    
    toggleOther(types, $('input[name="otherVal"]'));
    
    function toggleOther(selectField, otherInput){ 
        if (hasOtherSelected(selectField)) {
            otherInput.prop('disabled', false);
        } else {
            otherInput.prop('disabled', true);
        }
    }
    
    function hasOtherSelected(selectField) {
        return selectField.find('option:selected').filter(function(){
            return $(this).text() === 'other';
        }).length;
    }
});