Search code examples
jqueryniceforms

Mutually link Dropdown and text-input values


Looking for something that if my select with an id of contentPageID gets changed then it clears the value of the input with an id of itemURL and vice-versa. If a value is put for the input text then the val of the select turns to 0.


Solution

  • Here's one more way:

    $('#contentPageID, #itemURL').change ( function () {
    
        if ( $(this).is ('select') ) {
            $('#itemURL').val ("");
        }
        else {
            //--- Note that jQuery intelligently sets <select>s.
            $('#contentPageID').val (0);
        }
    } );
    

    See it in action.