I am looking to deactivate buttons when there are no selections made in the list boxes

Hello jQuery Experts, I am a beginner with jQuery and have a query. I have managed to enable the "Add category" button when any three values (one value per box) are selected from the above three boxes. However, I am struggling to enable the "Remove Category" and "Save Categories" buttons only if there are values in the fourth box. My current code is not achieving this functionality.

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>

Answer №1

attempt

$(document).ready(function(){
  if ($('#chosen-list-items').val() != undefined) {
    $('#exclude-category').prop("disabled", false);
    $('#store-categories').prop("disabled", false);
  } else {
    $('#exclude-category').prop("disabled", true);
    $('#store-categories').prop("disabled", true);
  }
});

Answer №2

You need to make the following update:

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>

Answer №3

To handle the event binding or utilize the SetInterval method in JQuery, you have a couple of options. (https://www.w3schools.com/jsref/met_win_setinterval.asp)

After setting the interval, the code will run 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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Eliminate any elements and their contents that come before a specific element

Is there a way to remove elements and content preceding a link inside a div when a user clicks a button? <div id="dialog" class="window"> //will be inserted a <select> element and few text here //but I want to clear them ...

Troubleshooting: AngularJS routing issue

I'm facing an issue with my AngularJS routing code. Here is the code snippet: /// <reference path="C:\Users\elwany\documents\visual studio 2015\Projects\spaapplication\spaapplication\scripts/angular.js" /& ...

Ways to incorporate color gradients into the corners of CSS cards

Below is an example of a basic bootstrap card: Example 1: https://i.stack.imgur.com/DK2Sz.png I attempted to draw blue and green colors side by side, but was only successful in drawing blue as shown below: Example 2: https://i.stack.imgur.com/CcSzP.png ...

How can we effectively override the automatic contrasting color determination of buttons based on their background in Boostrap v5.2 SCSS compilation?

Buttons in Bootstrap v5.2 dynamically adjust the text color based on the button color. Currently, I am modifying the bootstrap variables override file to change the primary color for my custom theme $green: #369d6d !default; $primary: $green !default; Ho ...

Animation does not occur after the stop() function is executed

My goal is to create a functionality where, upon moving back to the grey content box after leaving the button, the slideUp animation stops and the content slides down again. This works seamlessly with jQuery 1.x (edge), but when I switch to jQuery 1.10, th ...

When using AJAX with the JSONP datatype, a 404 error is encountered when there are no

Having a bit of trouble dealing with an unexpected AJAX response. The issue arises when I make a GET request to a website using AJAX and dataType jsonp. It's supposed to return data based on a number, but if no data is found for the given number, it ...

Tips for arranging two divs from a single column to display side by side on smaller screens instead of stacked on top of each other

My knowledge of CSS, HTML, and bootstrap is quite limited. I'm trying to figure out how to split a single column on a large screen into two columns on a smaller screen so that the content in two divs appears side by side instead of stacked on top of ...

Trigger a jQuery alert when a row containing a specific attribute is clicked

I am trying to work with an HTML table that looks like the one below: <table id='messagelist'> <tr id="rcmrowMTM" class="message selected" aria-selected="true"> <td class="threads"><span class="threads"></span&g ...

Validate if a string in JQuery contains a specific substring

How can I determine if one string contains another string? var str1 = "ABCDEFGHIJKLMNOP"; var str2 = "DEFG"; What function should I utilize to check if the string str1 includes the string str2? ...

Codeigniter shows error when attempting to invoke an ajax function

Looking for a way to validate a sign up form through ajax to confirm user input availability in a database. Found some code on a tutorial site but struggling to make it work. http://www.example.com/validation-tutorial Utilizing the Codeigniter framework, ...

Displaying inline-blocks with Bootstrap

I am currently working on a website project using HTML and Bootstrap. I'm encountering an issue with the side navigation bar as it's not displaying the two blocks inline but rather one after another. I've used the Bootstrap predefined class ...

Tips for Locating an Element by Its Attribute in JQuery

I currently have a variable that holds a list of selects, and my goal is to filter out only those with a 'dependsOn' attribute set to 'EventType'. My initial attempt below doesn't seem to work as intended and returns a count of 0: ...

What is the best way to use the width to fill in the empty space left by the column?

I'm still learning about bootstrap and I want to make sure that each column has the same size to fill up any empty space. Here is my attempt so far: <div class="container-fluid mt-3 position-absolute top-50"> <div class=&qu ...

Navigating to external URLs using a Form-based approach in .NET Framework

Is it possible to direct to an external website using a URL as a Command Argument? I attempted to redirect to facebook.com, but it resulted in http://localhost:58709/www.facebook.com%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20% ...

Using Angular 8 to Pass Form Data to Another Component via a Service

Is there a way to send all the Formgroup data as a Service in Angular to Another Component without using ControlValueAccessor? I want the receiver to automatically receive the value data whenever someone enters information on a form. I am attempting to mo ...

Troubleshooting JSONP Implementation with JQuery: Scope versus Async Problems

I've been trying to call a JSONP service using the $.ajax function: $.ajax({ url: jsonpURI, dataType: "jsonp", jsonpCallback: callback, success: function () { console.log("Success"); }, error: function (err) { ...

How come the width of an element with a relative position remains maximized even when it does not have any child elements?

I am facing an issue with a "message" element that needs to be resized if the text exceeds its width within certain limits, similar to how it works in messaging applications. .message { position: relative; color: white; background: #182533 ...

PHP/JQuery Validation Form Techniques

I am exploring ways to create a form with validation and data insertion into a MySQL database. Would creating the form in HTML, validating it using jQuery, and then submitting the data via PHP's post method be the optimal approach for this task? If a ...

Ensure proper spacing between the axis and text on your NVD3 charts by adding a space

My attempt to identify the classes responsible for styling the axis was unsuccessful. It left me feeling lost. I struggled to pinpoint the correct class to use in order to create some space between the axis and the text. Any suggestions on how I could ta ...

Transition of positions animation

Is there a way to animate the CSS relative position of a div element from left:-260px; to left: -130px; over 0.5s when hovering over it, and have it stay in that position as long as the mouse is on it? Then return to its original position when the mouse mo ...