1

How do I properly disable and enable form elements using jquery. I need to enable an input ("#byremaining") when I click on the "remaining" option. When i click the other options the input ("#byremaining") should be disabled again.

<html>

    <head>
    <script src="jq.js"></script>

    <script> 
    $(document).ready(function() {
    $('#byremaining').attr('disabled','disabled');
    });

    $(function(){

    $('#remaining').click(function(){
        $('#byremaining').removeAttr('disabled');
    });

    });
    </script>

    </head>
    <body>
    Sort by:
    <select name="plaats" id="plaats">
        <option value="plaats1">plaats1</option>
        <option value="plaats2">plaats2</option>
        <option value="plaats3">plaats3</option>
        <option value="plaats4">plaats4</option>
        <option value="remaining" id="remaining">remaining</option>
    </select>
    <br/>
    date range:<br/>

    Remaining:<input type="text" value="" name="byremaining" id="byremaining"></input><br/>

    </body>

    </html>

The remove attribute won't work now and i haven't created the disabled again.

6 Answers 6

3

$(document).ready(function() {
  $('#byremaining').prop('disabled', true); //use prop

  $('#plaats').change(function() {
    var selected = $('option:selected', this).attr('id')
    if (selected == 'remaining') { //if selected option is remaining 
      $('#byremaining').prop('disabled', false); //use prop
    } else {
      $('#byremaining').prop('disabled', true); //use prop
    }

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sort by:
<select name="plaats" id="plaats">
  <option value="plaats1">plaats1</option>
  <option value="plaats2">plaats2</option>
  <option value="plaats3">plaats3</option>
  <option value="plaats4">plaats4</option>
  <option value="remaining" id="remaining">remaining</option>
</select>
<br/>date range:
<br/>Remaining:

<input type="text" value="" name="byremaining" id="byremaining"></input>
<br/>

  1. Use change event for select
  2. Use .prop()
Sign up to request clarification or add additional context in comments.

6 Comments

where is toggle functionality?
$('#byremaining').prop('disabled', false); this part @SatyakiChatterjee use .prop()
Sorry I think this is not toggling.. because if you select remaining then it is enabled but you need to disable this for other selections.. may be I am missing something
@SatyakiChatterjee can you check again?
@guradio Thanks for the information, it was really useful!! I added something more now, when i click on the others (plaats1, plaats2, etc) then it is disabled again!
|
1

You can do it using change event of plaats:

$("#plaats").change(function() {
   var val = $(this).val();
   if(val == 'remaining') {
      $("#byremaining").removeAttr('disabled');
   } else {
      $("#byremaining").attr('disabled','disabled');
   }
});

Comments

1

Why not just toggle the state on the event trigger

if($("#byremaining").is("[disabled=disabled]"))
{
    $('#byremaining').removeAttr('disabled');
}
else
{
    $('#byremaining').attr('disabled','disabled');  
}

Comments

0

Use prop()

Disable:

("#byremaining").prop("disabled", true );

Enable:

("#byremaining").prop("disabled", false);

Comments

0

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <script src="jq.js"></script>

  <script>
    $(function() {
      $('#plaats').change(function() {
        var i = $(this).val();
        if (i == "remaining")
          $('#byremaining').prop('disabled', false);
        else
          $('#byremaining').prop('disabled', true);
      });
    });
  </script>

</head>

<body>
  Sort by:
  <select name="plaats" id="plaats">
    <option value="plaats1">plaats1</option>
    <option value="plaats2">plaats2</option>
    <option value="plaats3">plaats3</option>
    <option value="plaats4">plaats4</option>
    <option value="remaining" id="remaining">remaining</option>
  </select>
  <br/>date range:
  <br/>Remaining:

  <input type="text" value="" name="byremaining" id="byremaining"></input>
  <br/>

</body>

</html>

Comments

0
<html>

    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>


    <script> 
    $(document).ready(function() {
        $('#plaats').click(function(){
         var a =  $('#plaats').val();
            if (a == "remaining") {
                 $('#byremaining').attr('disabled','disabled');
             }
             else
             {
                 $('#byremaining').removeAttr('disabled');
             }
        });

    });


    </script>

    </head>
    <body>
    Sort by:
    <select name="plaats" id="plaats">
        <option value="plaats1">plaats1</option>
        <option value="plaats2">plaats2</option>
        <option value="plaats3">plaats3</option>
        <option value="plaats4">plaats4</option>
        <option value="remaining" id="remaining">remaining</option>
    </select>
    <br/>
    date range:<br/>

    Remaining:<input type="text" value="" name="byremaining" id="byremaining"></input><br/>

    </body>

    </html>

Comments

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.