Click here to Skip to main content
15,913,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two multicheckbox dropdown with id #dropdown1,#dropdown2 and json data 'filterdata'.When I change the dropdown values it bind result.I need to combine the two dropdown values and show the result.Sample code is


HTML
<pre lang="JavaScript">$(document).on("change", "#dropdown1", function () {
    var chklist = [];

    $("dropdown1:checked").each(function () {
        var cat = $(this).val();
        chklist.push(cat);

    });

    if (chklist.length != 0) {
        var Filtereditems = $.grep(Filterdata, function (element) {
            return chklist.indexOf(element.val1) > -1;
        });
        if (Filtereditems.length > 0) {
            $.each(Filtereditems, function (i) {
              //bind html data
            });
        } else {
            //clear result
        }
    }

});
$(document).on("change", "#dropdown2", function () {
    var chklist = [];

    $("#dropdown2":checked").each(function () {
        var cat = $(this).val();
        chklist.push(cat);

    });

    if (chklist.length != 0) {
        var Filtereditems = $.grep(Filterdata, function (element) {
            return chklist.indexOf(element.val2) > -1;
        });
        if (Filtereditems.length > 0) {
            $.each(Filtereditems, function (i) {
             //bind html data
            });
        } else {
           // clear result
    }

});



What I have tried:

I can show the result based on individual dropdown values.But i need result based on checked values in both dropdown.
Posted
Updated 20-May-16 1:11am

1 solution

place the array object outside the event, and remove it from both the events.
JavaScript
var chklist = [];


check this demo: Edit fiddle - JSFiddle[^]
 
Share this answer
 
v2
Comments
Dil0500 20-May-16 7:16am    
filtereditems is different that based on chklist.
Karthik_Mahalingam 20-May-16 7:21am    
try using only one event and combine the code..
on("change", "#dropdown1,#dropdown2", function () {
Dil0500 20-May-16 7:23am    
var filterdata is json data
Karthik_Mahalingam 20-May-16 7:29am    
seems syntax error [ $("#dropdown2":checked").each(function () { ]

corrected one
$("#dropdown2:checked").each(function () {
Dil0500 20-May-16 7:42am    
ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900