The disablement of the button is a result of concealed select elements

I'm facing an issue with a form that contains three select options:

  • Fit
  • Colour
  • Size

By default, the 'Fit' dropdown and 'Colour' dropdown are active with a default value selected (e.g. Regular Fit and Blue Colour).

There are multiple 'Size' dropdowns, but only one is visible at a time based on the selection made in the 'Fit' dropdown.

The Button remains disabled if the option value is set to "none".

Challenge

The Button only becomes active when all three 'Size' dropdowns have been modified to a value other than "none" (this involves selecting an initial size for Regular, then choosing Petite and Long from the 'Fit' dropdown). Ideally, I want the button to consider only the active 'Size' dropdown.

Solution Update

A helpful solution was provided on jsFiddle by @nagappan, many thanks for that.

https://jsfiddle.net/dodgers76/c0dvdwbz/

var currentSelectedVals = {'selector-fit':'','selector-color':'','selector-sizes':''};
var disableComboVals = [
{'selector-fit':'','selector-color':'','selector-sizes':'none'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'10'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'20'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'22'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'24'},
{'selector-fit':'long','selector-color':'','selector-sizes':'22'},
{'selector-fit':'long','selector-color':'','selector-sizes':'24'}
];
function checkDisableCombo() {
return $.grep(disableComboVals, function(vals){
  cnt = 0;
  $.each(vals, function(key,val) {
     console.log('comparing key val '+key+val);
     if (val === '' || val === currentSelectedVals[key]) {
         console.log('>>matched values');
         cnt = cnt + 1;
     }
  });
  if (cnt===3) {
     return true;
  }
  return false;
});
};
$(function(){
var sizeVal = 'none';


  $("select.selector-fit").on("change", function(){
    //remove active
    $("select.selector-sizes.active").removeClass("active");
    //check if select class exists. If it does then show it
    var subList = $("select.selector-sizes."+$(this).val());
    if (subList.length){
      //class exists. Show it by adding active class to it
      subList.addClass("active");
      subList.val(sizeVal);
    }
  });

  $('.selector-sizes').on('change', function() {
    sizeVal = $(this).val();
  });
});

$(function() {
  $('.selector').on('change', function() {
    var $sels = $('option.selector-sizes:selected[value="none"]');
    var isSizeSelector = jQuery.inArray( "selector-sizes",this.classList);
    currentSelectedVals[this.classList[1]] = this.value;
    console.log(currentSelectedVals);
    var result = checkDisableCombo();
    console.log(result);
    if ( result.length > 0) {
        console.log('disabled false');
        $("#Testing").attr("disabled", true);
    } else {
        $("#Testing").attr("disabled", false);
    }
  }).change();
});

Answer №1

To effectively disable the button based on selected values from multiple drop downs, we can implement a solution using a global variable to keep track of the current selections. By maintaining an array of disabled combinations, we can compare the user's choices with these combos and disable the button accordingly. The validation process can be achieved by checking for matches within the combinations. Check out the updated JS Fiddle link for more details. UPDATED JS FIDDLE

function checkDisableCombo() {
return $.grep(disableComboVals, function(vals){
  cnt = 0;
  $.each(vals, function(key,val) {
     console.log('comparing key val '+key+val);
     if (val === '' || val === currentSelectedVals[key]) {
         console.log('>>matched values');
         cnt = cnt + 1;
     }
  });
  if (cnt===3) {
     return true;
  }
  return false;
});

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

Error message displayed in Wordpress: "An uncaught SyntaxError was encountered, with an unexpected token &

I have added scripts to a child theme using the following code... function child_theme_scripts() { wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/custom-child.js', array(), '1.0.0&a ...

Limiting cross-origin resource sharing to only mobile browsers

Despite successfully adding CORS headers to my server's response object, I am facing a problem where mobile browsers are not displaying the data from ajax calls, showing a CORS error instead. Any insights on why this issue might be occurring? CLIENT ...

Problem with email verification process

Hey there, I'm currently working on validating an email address using regular expressions. Here is the code snippet I'm using: <input type="text" name="email" id="email"/> var email = $("input#email"), re = /^[A-Za-z ...

Here is a unique rewrite of the text: "Discover how to efficiently sort through a JSON file using AngularJS. By

Seeking to implement a search feature using angularjs that filters results based on Name and Door-No. Once the filter is applied, a list of filtered Names should be displayed, with details shown upon clicking each name. See below for the code snippet: JSO ...

Issue with Django Ajax repeatedly submitting identical data to the endpoint

Having trouble posting the value of an HTML item (the id of the item) to the view in order to add it to the cart. Despite different values being displayed in the HTML source, it always posts the value of the last item generated by the Django {% for %} loop ...

jQuery can be used to obtain the label for a checkbox with a particular value

Currently, I am facing an issue with retrieving the label for a checkbox using jQuery. Let me provide you with the relevant HTML code: <div class="checkbox"> <label><input type="checkbox" name="cb_type[]" value="sold" >Sold</label ...

Eliminate entry upon checkbox completion from task schedule

Currently working on developing my own to-do list web application. I have set everything up except for one thing. I am trying to figure out how to remove a checkbox from the array when it is checked. Can someone please assist me in achieving this using DOM ...

HTTPInterceptor failing to capture incoming HTTP requests from external widget in Angular

Within my application, I incorporate a third-party UI widget obtained from the npm registry as a dependency. This widget makes a GET request using the axios module when integrated into my app's user interface. However, I have observed that the HTTP G ...

PHP offers a different solution instead of using an HTML hidden input tag

Is there a way to pass a value from one HTML form to another without using hidden input fields? I need this value for a prepared SQL statement. I'm concerned about security risks associated with using hidden input fields. Does anyone have an alternat ...

The Spring MVC Ajax Date Field encountered an error due to incorrect syntax in the request sent by the client

We encountered the following error: The request sent by the client was syntactically incorrect. with the following code snippet: <%@page language="Java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.su ...

What is the best approach to connecting a listener to a date selection in the Django admin interface?

My Django admin page features a model with a DateField called 'date': # models.py class Article(models.Model): date = models.DateField( help_text="Article publication date" ) # admin.py @admin.register(Article) class ArticleAdm ...

Is it possible to incorporate FCM into Next.js and effectively handle server-side events for FCM on Next.js servers?

Is it feasible to incorporate the Firebase Cloud Messaging (FCM) into my upcoming Next.js application? Can I manage both client-side and server-side modules within the server side? ...

Basic authentication for cross-domain ajax requests

I'm currently working on a cross-domain ajax request in order to retrieve some important data. The REST service I am connecting to requires Basic authentication, which is configured through IIS. $.ajax({ type: "GET", xhrFields ...

The image does not resize vertically to fit the browser window

How can I make a large background image stretch to the size of the browser window when a user first visits the site, similar to how it is done on ? The current code I am using stretches horizontally but won't stretch vertically past a certain min-heig ...

Leverage htaccess functionality to modify website configurations without altering the URL

I currently have the following htaccess configuration: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ dl.php?id=/$1 [QSA,L] </IfModule> It works perfectly fine. However, I am facing a problem with my site URLs and images ...

Tips for setting unique click functions for each face of a cube using React Three Fiber

Hey there! I have created a basic Cube component using react three fibre. import {useState,useRef} from 'react'; import { useFrame } from '@react-three/fiber'; const Box = ({props}) =>{ const ref = useRef() const [hove ...

Retrieving the width and height of a DIV element using Javascript

Currently, I am attempting to retrieve the width and height of a div element as it is being changed by the user and then pass that data to another page. However, I am encountering difficulties in obtaining the width and height values. <script> $(fun ...

Can the tooltip placement be adjusted in ng-bootstrap when it reaches a specific y-axis point?

Currently, I am facing an issue with my tooltip appearing under the header nav-bar instead of flipping to another placement like 'left-bottom' when it reaches the header. Is there a way to manually set boundaries for tooltips in ng-bootstrap? Unl ...

What is the best way to cancel an interval set by a function within a functional component?

When using useEffect, it's easy to clear an interval like this: useEffect(() => { const interval = setInterval(some function, time); return () => clearInterval(interval) }) But what if I need to set an interval inside a function? Do I hav ...

What is the best way to display all checked checkboxes even when the page is reloaded?

I am facing an issue with my website - it is using JavaScript to search for checked checkboxes. $(function () { var $allELements = $('.input-box'); var $selectedElementsListing = $('#selectedElements'); var $selec ...