jQuery - Disable Form Fields

JqueryForms

Jquery Problem Overview


I have 2 fields: 1 input, 1 select box.

Is it possible to make the browser disable one of the fields when the user uses the other?

For example, if the user is entering data into the input, the select box is disabled. If the user is selecting data from the select (dropdown), the input is disabled.

Any help with this would be much appreciated.

Cheers in advance.

Jquery Solutions


Solution 1 - Jquery

<script type="text/javascript" src="jquery.js"></script>          
<script type="text/javascript">
  $(document).ready(function() {
      $("#suburb").blur(function() {
          if ($(this).val() != '')
              $("#post_code").attr("disabled", "disabled");
          else
              $("#post_code").removeAttr("disabled");
      });

      $("#post_code").blur(function() {
          if ($(this).val() != '')
              $("#suburb").attr("disabled", "disabled");
          else
              $("#suburb").removeAttr("disabled");
      });
  });
</script>

You'll also need to add a value attribute to the first option under your select element:

<option value=""></option>

Solution 2 - Jquery

The jQuery docs say to use prop() for things like disabled, checked, etc. Also the more concise way is to use their selectors engine. So to disable all form elements in a div or form parent.

$myForm.find(':input:not(:disabled)').prop('disabled',true);

And to enable again you could do

$myForm.find(':input:disabled').prop('disabled',false);

Solution 3 - Jquery

$('input, select').attr('disabled', 'disabled');

Solution 4 - Jquery

try this

$("#inp").focus(function(){$("#sel").attr('disabled','true');});

$("#inp").blur(function(){$("#sel").removeAttr('disabled');});

vice versa for the select also.

Solution 5 - Jquery

Here's a jquery plugin to do the same: http://s.technabled.com/jquery-foggle

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser159025View Question on Stackoverflow
Solution 1 - JqueryMatt HowellView Answer on Stackoverflow
Solution 2 - JqueryMetaSkillsView Answer on Stackoverflow
Solution 3 - JqueryRigobert SongView Answer on Stackoverflow
Solution 4 - JqueryNrjView Answer on Stackoverflow
Solution 5 - JqueryRohit balView Answer on Stackoverflow