Alukkupaiyan
Alukkupaiyan

Reputation: 95

I want to disable buttons when nothing is selected in the list boxes

Hi Experts I'm new to jQuery. I have enabled the "Add category" button if any three values(one value per each box) are selected from the above three boxes. I want to enable the "Remove Category" & "Save Categories" button only if there is any values in the fourth box But, My code is not working for this.

var one = $('.select-manage-category').val();
var two = $('.select-manage-category1').val();
var three = $('.select-manage-category2').val();


$('.select-manage-category, .select-manage-category1, .select-manage-category2').change(function() {
  var one = $('.select-manage-category').val();
  var two = $('.select-manage-category1').val();
  var three = $('.select-manage-category2').val();
  if (one && two && three) {
    $("#add-category").prop("disabled", false);
  } else {
    $("#add-category").prop("disabled", true);
  }

});

$('#selected-lst-values').change(function() {
  if ($(this).val()) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }
});

$('#add-category').click(function() {
  $(
    '.select-manage-category, .select-manage-category1, .select-manage-category2'
  ).each(function() {
    $('#selected-lst-values').append('<option value="' + $(this).val() + '">' + $(this).val() + '</option>');
  });
});
$('#remove-category').click(function() { // check change here
  var select = document.getElementById('selected-lst-values');

  for (var i = 0; i < 3; i++) {
    select.removeChild(select.lastChild);
  }
    if ($('#selected-lst-values').val()) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }



});
$('#selected-lst-values>option').on("change", function() {
  if ($(this).val() === '') {
    $('#mnage-category-savebtn').attr({
      disabled: disabled
    });

  } else {
    $('#mnage-category-savebtn').removeAttr('disabled');
  }
});
.select-manage-category,
.select-manage-category1,
.select-manage-category2 {
  width: 100px;
  float: left;
  margin-right: 4px;
}

p {
  clear: left;
  text-align: center;
}

#selected-lst-values {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><select class="form-control select-manage-category" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
</select></div>

<div><select class="form-control select-manage-category1" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
		</select></div>
<div><select class="form-control select-manage-category2" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
		</select>
</div>
<p class="text-center color-red">You can add up to 20 categories</p>
</div>
<input id="add-category" name="add" type="button" value="Add Category" disabled>
<input id="remove-category" name="add" type="button" value="Remove Category" disabled>
<div><select id="selected-lst-values" class="form-group percent-100" size="5">
		</select></div>
<button id='save-categories' class="btn btn-md btn-radius pi-btn prodetails-btn" type="button" disabled><strong>Save Categories</strong> 
    <span class="glyphicon glyphicon-menu-right right-arrow-head-icon"></span>
</button>

Upvotes: 0

Views: 241

Answers (3)

Adil Hanif
Adil Hanif

Reputation: 48

Either you can bind to event or use SetInterval method of JQuery. (https://www.w3schools.com/jsref/met_win_setinterval.asp)

once you set interval it will execute every 3 seconds

setInterval(function(){  
    if ($('#selected-lst-values').val!=null) {
      $('#remove-category').prop("disabled", false);
      $('#save-categories').prop("disabled", false);
    } 
    else {
      $('#remove-category').prop("disabled", true);
      $('#save-categories').prop("disabled", true);
    } }, 3000);

Upvotes: 0

bhansa
bhansa

Reputation: 7504

You just have update this :

if (one && two && three) {
    $("#add-category").prop("disabled", false);
    $("#remove-category").prop("disabled",false);
  }

var one = $('.select-manage-category').val();
var two = $('.select-manage-category1').val();
var three = $('.select-manage-category2').val();


$('.select-manage-category, .select-manage-category1, .select-manage-category2').change(function() {
  var one = $('.select-manage-category').val();
  var two = $('.select-manage-category1').val();
  var three = $('.select-manage-category2').val();
  if (one && two && three) {
    $("#add-category").prop("disabled", false);
  } else {
    $("#add-category").prop("disabled", true);
  }

});

$('#selected-lst-values').change(function() {
  if ($(this).val()) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }
});

$('#add-category').click(function() {
  $(
    '.select-manage-category, .select-manage-category1, .select-manage-category2'
  ).each(function() {
    $('#selected-lst-values').append('<option value="' + $(this).val() + '">' + $(this).val() + '</option>');
  });
});
$('#remove-category').click(function() { // check change here
  var select = document.getElementById('selected-lst-values');

  for (var i = 0; i < 3; i++) {
    select.removeChild(select.lastChild);
  }
    if ($('#selected-lst-values').val()) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }



});
$('#selected-lst-values>option').on("change", function() {
  if ($(this).val() === '') {
    $('#mnage-category-savebtn').prop('disabled',true)

  } else {
    $('#mnage-category-savebtn').removeAttr('disabled');
  }
});
.select-manage-category,
.select-manage-category1,
.select-manage-category2 {
  width: 100px;
  float: left;
  margin-right: 4px;
}

p {
  clear: left;
  text-align: center;
}

#selected-lst-values {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><select class="form-control select-manage-category" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
</select></div>

<div><select class="form-control select-manage-category1" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
		</select></div>
<div><select class="form-control select-manage-category2" size="5">
    <option>1</option>
    <option>2</option>
    <option>1</option>
    <option>2</option>
		</select>
</div>
<p class="text-center color-red">You can add up to 20 categories</p>
</div>
<input id="add-category" name="add" type="button" value="Add Category" disabled>
<input id="remove-category" name="add" type="button" value="Remove Category" disabled>
<div><select id="selected-lst-values" class="form-group percent-100" size="5">
		</select></div>
<button id='save-categories' class="btn btn-md btn-radius pi-btn prodetails-btn" type="button" disabled><strong>Save Categories</strong> 
    <span class="glyphicon glyphicon-menu-right right-arrow-head-icon"></span>
</button>

Upvotes: 0

ARr0w
ARr0w

Reputation: 1731

try

$(document).ready(function(){
  if ($('#selected-lst-values').val() != null) {
    $('#remove-category').prop("disabled", false);
    $('#save-categories').prop("disabled", false);
  } else {
    $('#remove-category').prop("disabled", true);
    $('#save-categories').prop("disabled", true);
  }
});

Upvotes: 1

Related Questions