Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable input fields in Bootstrap

I am having 3 input fields in Bootstrap, if any one of the input field is filled, I want to disable other two.

Lets say I am having A,B,C input boxes.
If A is filled then B & C will become disabled or readonly and vice versa.

Also if I delete value from A then B & C again become enabled. As B & C was also not filled.

like image 298
Wisher Well Avatar asked Mar 25 '15 10:03

Wisher Well


People also ask

How do I disable a form in HTML?

Disabling all form elements HTML form elements have an attribute called disabled that can be set using javascript. If you are setting it in HTML you can use disabled="disabled" but if you are using javascript you can simply set the property to true or false .

What is form control in bootstrap?

Bootstrap form-control class is use to make an element fit it container <div> <input type='text' class='form-control'> </div> div{ width: 400px; background: gray; }

What is form inline in bootstrap 5?

Inline formsUse the .row-cols-* classes to create responsive horizontal layouts. By adding gutter modifier classes, we'll have gutters in horizontal and vertical directions. On narrow mobile viewports, the .col-12 helps stack the form controls and more.


2 Answers

You simply do a jQuery function

// #your_filled_input is for the id of the input
$("#your_filled_input").keyup(function() {
    if ($("#your_filled_input").val().length >= 0) {
      $("#your_first_other_field" ).attr('disabled', 'disabled');
      $("#your_second_other_field").attr('disabled', 'disabled');
    }
});
like image 152
Sileax Avatar answered Oct 16 '22 06:10

Sileax


$("#fieldA").keyup(function() {
  if ($("#fieldA").val().length > 0) {
    $("#fieldB").attr('disabled', 'disabled');
    $("#fieldC").attr('disabled', 'disabled');
  } else {
    $('#fieldB').removeAttr('disabled');
    $('#fieldC').removeAttr('disabled');
  }
});

$("#fieldB").keyup(function() {
  if ($("#fieldB").val().length > 0) {
    $("#fieldA").attr('disabled', 'disabled');
    $("#fieldC").attr('disabled', 'disabled');
  } else {
    $('#fieldA').removeAttr('disabled');
    $('#fieldC').removeAttr('disabled');
  }
});

$("#fieldC").keyup(function() {
  if ($("#fieldC").val().length > 0) {
    $("#fieldB").attr('disabled', 'disabled');
    $("#fieldA").attr('disabled', 'disabled');
  } else {
    $('#fieldB').removeAttr('disabled');
    $('#fieldA').removeAttr('disabled');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='fieldA' />
<input type='text' id='fieldB' />
<input type='text' id='fieldC' />
like image 3
Wisher Well Avatar answered Oct 16 '22 04:10

Wisher Well