56

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.

0

5 Answers 5

114
<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>
Sign up to request clarification or add additional context in comments.

7 Comments

Hey bigmattyh, Thanks for you quick reply. Unfortunately, the script isn't working for me. Is there something I need to be doing in order for the script to work? Below is my code: <input class="textfield" type="text" name="post_code" id="post_code" maxlength="4" /> <select size="6" name="suburb" id="suburb" /> <option selected="selected"></option> </select>
Well, you'll need to include jQuery, and put this code into a $(document).ready() block (updated to relfect this). As for your HTML, you will need a default value on that select box, to make this work reliably.
Thanks for you efforts. Unfortunately, it is not working for me. Cheers.
Maybe you could post more of your code. It's awfully hard to diagnose what's wrong without seeing the code.
Again thank you. Perhaps I didn't accurately describe what I am trying to achieve. As you know there are 2 fields: input, select. when one is entering data into the input, the select field should be disabled. The same for when selecting from the select field. The issue is, one has to 'click off' the fields for the other to become 'abled' again. Below is my code: <input class="textfield" type="text" name="post_code" id="post_code" maxlength="4" /> <select size="6" name="suburb" id="suburb"> <option selected="selected" value=""></option> </select> Cheers.
|
48

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);

2 Comments

this is cleaner and easier to include in any logical constructs
With jQuery 1.6+ this is the recommended approach.
12
$('input, select').attr('disabled', 'disabled');

1 Comment

+1 and i add the following line for style : $('input, textarea').attr('readonly', 'readonly');
7

try this

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

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

vice versa for the select also.

2 Comments

can u add details as to what happened ? I tried it in IE was working fine.
Hey Nrj. Thanks for replying. I added the script. But nothing happens. I've added $(document).ready(function() and tried different ID names, but nothing happened. Cheers.
0

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

1 Comment

costs between $3 or $15 and is not Open Source, but it does solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.