Any field change in form, disable button, and activate another I have JavaScript code that activates a button and deactivates another if a select is changed.

HTML

&lt;select name="list" id="list"&gt;
     &lt;option value=""&gt;choose&lt;/option&gt;
     &lt;option value="1"&gt;1&lt;/option&gt;
     &lt;option value="1"&gt;2&lt;/option&gt;
     &lt;option value="3"&gt;3&lt;/option&gt;                
&lt;/select&gt;
&lt;input name="refresh" type="button" value="refresh" disabled="" /&gt;
&lt;input name="add" type="button" value="add"  /&gt;


JavaScript

$('select').change(function () {
    var op = $(this).val();
    if (op != '') {
        $('input[name="refresh"]').prop('disabled', false);
    } else {
        $('input[name="refresh"]').prop('disabled', true);
    }
});
$('select').change(function () {
    var op = $(this).val();
    if (op != '') {
        $('input[name="add"]').prop('disabled', true);
    } else {
        $('input[name="add"]').prop('disabled', false);
    }
});


I need to expand the function to all fields in the form with minimal code. I could use the same code for the rest of inputs, but the code will be very long. I'm looking for a short version.
Targetting elements

$('select') is generic selector and will select all &lt;select&gt; elements on the page. There might be some &lt;select&gt; elements which are not needed to target for this functionality.

You can target only required elements using


Unique parent selector + descendant selector

In this, there will be a main container element and using this all the descendant &lt;select&gt; elements can be targeted.

Example:

HTML:

&lt;div id="container"&gt;
    &lt;select name="list" id="list"&gt;
        &lt;option value=""&gt;...&lt;/option&gt;
    &lt;/select&gt;
    &lt;input name="refresh" /&gt;
    &lt;input name="add" type="button" value="add" /&gt;

    &lt;select name="list1" id="list1"&gt;
        &lt;option value=""&gt;...&lt;/option&gt;
    &lt;/select&gt;
    &lt;input name="refresh1" /&gt;
    &lt;input name="add1" type="button" value="add1" /&gt;

    ...
&lt;/div&gt;


JavaScript:

$('#container select').change(function() {

Using common class
Sometimes, adding all the elements under a container is not possible. In such cases, adding a common class to all the elements will allow to select elements using class selector.

$('.someCommonClass').change(function() {


Common attributes can also be added and selected using Attribute Equals Selector [name=”value”]. However, this can be slower than class selectors.


Similarly, another common + unique class should be applied to buttons.

&lt;input name="refresh" class="anotherUnique" /&gt;
&lt;input name="add" class="anotherUnique" /&gt;


Then, to disable/enable these buttons, they can be referenced.

HTML5 data-* attributes

HTML5's custom data-* attribute can be used to store custom data on HTML elements.

&lt;select name="list" id="list" data-key="value"&gt;


Getting data-* attribute value

jQuery provides data(name) method to get the value of the data-name attribute value. Core JavaScript has dataset property that allows access to data attributes.

$('elemSelector').data('key')


Using prop() with value

prop() accepts second parameter which will be converted to Boolean internally and depending on the value, the action will be taken. So, the if and else statements in the question can be combined as

$('elemSelector').prop('disabled', booleanValueHere);


If second parameter is truthy, element will be disabled. If falsy then element will be enabled.

Putting it all together



// Bind change event on all select elements
// having unique class
$('select.unique').change(function () {
    // Cache this
    var $this = $(this);

    // Get the data-* attribute values
    var button1 = $this.data('button1'),
        button2 = $this.data('button2');

    // Get the value of this select box
    // Use this as second parameter to prop
    $('input[name="' + button1 + '"]').prop('disabled', this.value);
    $('input[name="' + button2 + '"]').prop('disabled', !this.value);
});
/* For Demo Purpose */
input {
  color: green;
  font-weight: bold;
}
input:disabled {
  color: gray;
  opacity: .5;
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

&lt;select name="list" id="list" class="unique" data-button1="refresh" data-button2="add"&gt;
    &lt;option value=""&gt;choose&lt;/option&gt;
    &lt;option value="1"&gt;1&lt;/option&gt;
    &lt;option value="2"&gt;2&lt;/option&gt;
    &lt;option value="3"&gt;3&lt;/option&gt;
&lt;/select&gt;
&lt;input name="refresh" type="button" class="anotherUnique" value="Refresh" /&gt;
&lt;input name="add" type="button" class="anotherUnique" value="Add" disabled /&gt;

&lt;hr /&gt;
&lt;!-- Another select box --&gt;
&lt;select name="list1" id="list1" class="unique" data-button1="refresh1" data-button2="add1"&gt;
    &lt;option value=""&gt;choose&lt;/option&gt;
    &lt;option value="10"&gt;10&lt;/option&gt;
    &lt;option value="20"&gt;20&lt;/option&gt;
    &lt;option value="30"&gt;30&lt;/option&gt;
&lt;/select&gt;
&lt;input name="refresh1" type="button" class="anotherUnique1" value="Refresh 1" /&gt;
&lt;input name="add1" type="button" class="anotherUnique1" value="Add 1" disabled /&gt;




Conclusion

Now, the JavaScript code don't have to be written again for each new select element. Just adding the markup with required attributes will automatically work. The relation between HTML element-JavaScript handler is reduced from many to many to many to one.