Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active September 3, 2015 15:43
Show Gist options
  • Select an option

  • Save christopherbauer/d8494c63386c60c4535d to your computer and use it in GitHub Desktop.

Select an option

Save christopherbauer/d8494c63386c60c4535d to your computer and use it in GitHub Desktop.
function disableChecker(checkSelector, disableValue, targetSelector) {
var optionValue = $(checkSelector).val();
if (optionValue === disableValue) {
$(targetSelector).prop("disabled", true); //Disable target
} else {
$(targetSelector).prop("disabled", false); //Enable target
}
}
function DisableIf(checkSelector, disableValue, targetSelector) {
$(checkSelector).on("change keyup blur", function() {
disableChecker(checkSelector, disableValue, targetSelector);
});
disableChecker(checkSelector, disableValue, targetSelector);
}
<select id="disableSource">
<option value="0">Disable Next</option>
<option value="1">Enable Next</option>
</select>
<input type="text" id="disableTarget" />
<input type="text" id="disableTarget2" />
<select id="disableSource">
<option value="0">Disable Next</option>
<option value="1">Enable Next</option>
</select>
<input type="text" id="disableTarget" />
<input type="text" id="disableTarget2" />
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
//Please note that at this time, order is important!
DisableIf("#disableSource", "0", "#disableTarget");
DisableIf("#disableTarget", "Disable Next", "#disableTarget2");
DisableIf("#disableSource", "0", "#disableTarget2");
<script>
DisableIf("#disableSource", "0", "#disableTarget");
DisableIf("#disableTarget", "Disable Next", "#disableTarget2");
DisableIf("#disableSource", "0", "#disableTarget2");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment