Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable an input field if second input field is filled

totally a newbie... I just want to know how to dynamically disable an input field when the second input field is filled

eg:

<td><input type="text" name="num-input1" id="dis_rm"  value=""></input></td>
<td><input type="text" name="num-input2" id="dis_per" value="" ></input></td>

pls... any links and hints will do...

like image 260
debrajsy Avatar asked Jul 21 '11 04:07

debrajsy


People also ask

How disable button if field is empty?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

What specifies that an input field should be disabled?

The disabled attribute for <input> element is used to specify that the input field is disabled. A disabled input is un-clickable and unusable. It is a boolean attribute. The disabled <input> elements are not submitted in the form.

How do I disable a field in HTML?

Create an HTML table using the <table> element. Now add the <form> element within this table. Next, we will create form fields. We add the required form fields to the form using the <tr> element that is used to add rows to a table.


1 Answers

You simply need to give it a disabled property:

document.getElementById("dis_rm").disabled = true;
document.getElementById("dis_per").disabled = true;

you can use the on change event to see if one of them is filled:

var dis1 = document.getElementById("dis_rm");
dis1.onchange = function () {
   if (this.value != "" || this.value.length > 0) {
      document.getElementById("dis_per").disabled = true;
   }
}

so if the first one is filled, the second one will be disabled

like image 76
Ibu Avatar answered Sep 26 '22 01:09

Ibu