Search code examples
jquerydynamic-forms

How to loop through all dropdown menus to check if they have a certain selected value?


I asked this question previously: Disabling and Re-Enabling Dynamic Form Elements with JQuery but I feel that I was perhaps asking the wrong question.

Please see this fiddle for an easier way to understand my problem: http://jsfiddle.net/5ubTe/12/

I already have a function to check when the tags drop down is changed and what I am looking to do is to check all dropdowns of the class dropdown and dropdownclone and check if any of their selected values are tags.

If any of them have that value then disable the submit button by calling the function, if none of them have the tags value then enable the submit button for the form to be processed.

Hope that makes sense!

Thanks for any help given.

UPDATE

Latest fiddle: http://jsfiddle.net/5ubTe/14/ but doesn't work... yet.


Solution

  • You should be able to use a selector for this an tranverse the dropdowns to check for the selected options. Then check the length of the result to see if any were matched.

    var selected= $('.dropdown, .dropdownclone').find('option[value=tags]:selected');
    
    if(selected.length > 0) {
        disableSubmit();
    }
    else {
        enableSubmit(); 
    }