Search code examples
jqueryeventsinputdisabled-input

clicking or changing input, disables others


$(function() {
    $('.disablesothers').on('change focus click', function () {
	  var $input = $(this).find('input');
	  var $brothers = $input.parent().siblings('.disablesothers');
	  console.log($brothers.length);
      $input.removeAttr('disabled');
	  $brothers.find('input').attr('disabled', 'disabled');
    });
});
input:disabled {
   border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="overlabel tab_content default tooltip disablesothers" title="Bind text">
  <span>Destination name, zone</span>
  <input type="text" name="hotel_name">
</label>
<label class="overlabel disablesothers">
  <input type="text" name="other_hotels" id="other_hotels">
</label>

Note that I am binding the event on the label, not the inputs,

But this don't work

Any idea why?

-EDIT-

This could be because removeAttr it's not actually removing it, just clears the value.. ?


Solution

  • I solved by adding a transparent div wich covers the label when the input is disabled, because some browsers won't fire the event if it's a disabled input,

    <label class="overlabel disablesothers">
      <input type="text" name="other_hotels" id="other_hotels">
    </label>
    

    then,

    :checked + div {
        display: inline-block;
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;
    }