I have a form and use Jquery to create an array called “fields” when an input is changed:
var fields = $(":input").serializeArray();
I then use this array in an .each function to display a list of items:
jQuery.each(fields, function(i, field){
//get multiple values into array and perform action for each
val = field.value;
name = field.name;
var valarray = val.split(',');
for (var i in valarray) {
//get each answer
val = $.trim(valarray[i]);
$("#pid"+val).show();
}//end for
});//end each
At the moment the console.log shows this if 3 radio buttons are selected:
[Object { name= "question1" , value= "aa,bb,cc,dd" }, Object { name= "question2" , value= "bb,cc" }, Object { name= "question3" , value= "cc,ee" }]
And all values aa, bb, cc, dd and ee are shown.
What I would like to do, rather than showing all values, is compare the values of each question and only show the ones that appear in ALL questions (cc in this example). The idea is to filter the list so if only question1 and question2 have been selected, “bb” and “cc” would be shown. Further to that, if a 4th question is selected with the value “dd” and there isn’t therefore a value that matches all the questions, I’d like to return an alert and remove/deselect the last value, therefore reverting back to just “cc”.
I can’t work out how to compare the values to show only matching ones. I’ve used code from a few answers on stackoverflow and I’ve tried counting instances but seem to be going round in circles so any help would be greatly appreciated.
I hope I’ve given you enough info. Thanks.
I don’t know whether I managed to catch everyone out or if this was just missed but I worked out a solution by counting the occurences of each answer and where this equals the amount of questions asked (ie, the answer appears for all questions), adding it to another array. Thanks anyway!