tags:

views:

4434

answers:

4

Hey all,

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.

+8  A: 
<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").attr("disabled", "");
      });

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

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

<option value=""></option>
bigmattyh
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.
bigmattyh
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.
bigmattyh
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.
This may not work, if only because the disabled attribute works by its presence. Setting it to true or false will still have the same effect, which is to disable the element. Better to use removeAttr when you want to enable the element in question.
David Andres
@dandres109: Ah, yes, true. Typed it in a hurry.
bigmattyh
+4  A: 

try this

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

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

vice versa for the select also.

Nrj
Thanks for you efforts. Unfortunately, it doesn't work.
can u add details as to what happened ? I tried it in IE was working fine.
Nrj
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.
+2  A: 
$('input, select').attr('disabled', 'disabled');
Rigobert Song
A: 

Hey it worked like charm. Thanks for the code. Time saving.

sanjay