newbieRB
newbieRB

Reputation: 133

Disable all formfields except 1 field after change a select box

if a user change the option of a selectbox i want to disable all the fields inside the form without the triggering select-box and one specific input-field with id=xyz.

How i can do it in the fast and easiest way.

"#newsite_IS_HELPER" is the id of the selectbox

$('#newsite_IS_HELPER').change(function(){
    alert('What i have to do?');
});

Upvotes: 0

Views: 63

Answers (2)

Yogesh Mistry
Yogesh Mistry

Reputation: 2152

You can do that by selecting all input elements under your form, and excluding what you don't want to select i.e. this element (one which is triggering the disabling action i.e. select in your case) and the input element with #xyz.

$('#your_form_id :input:not(#xyz):not(#newsite_IS_HELPER)').prop("disabled", true);

$('#newsite_IS_HELPER').change(function(){
    var this_val = $(this).val();
    var disable_all = false;
    if(this_val != "1"){
        disable_all = true;
    }
    $('#your_form_id :input:not(#xyz):not(#newsite_IS_HELPER)').prop("disabled", disable_all );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="your_form_id">
  <input id="xyz">
  <input><input><input><input><input>
  <select id="newsite_IS_HELPER">
  <option value="1">enable all</option><option value="2">disable all</option><option value="3">disable all again</option>
  </select>
</form>

Upvotes: 1

Basanta Matia
Basanta Matia

Reputation: 1562

How I do,

$ ('#newsite_IS_HELPER').change(function(){ 
   $("input").attr("disabled", "disabled");       
   $(this).attr("disabled",false);
   $("#xyz").attr("disabled",false);
 });

Or-else, you can use prop in place of attr, like bellow my code,

$("input").prop('disabled', true);

Upvotes: 0

Related Questions