views:

71

answers:

3
A: 

Rather than test for equality, I think the better means is to check whether or not each of your values are in the array using the jQuery inArray function.

Granted, this is just the beginning of code. You could probably write a function to shore this up, like so.

function radioSelected(val) {
  return ($.inArray(val, laArray) != -1);
}

and adapt it to your existing script.

villecoder
i have tried to add the HTML of the form, but I got an error saying I can't add that much without credits....
tschardak
thank you for the reaction, villecoder.unfortunately, to a beginner like me you have to draw it ;)the way I thought it would work the best is if I compare the choices against all possible of arrays/all possible combinations of selected radios, rather than check one by one. because every choice has too many advices that go with it...
tschardak
A: 

You cannot compare arrays this way you should probably either compare each element of the 2 arrays

function compare_array(array1,array2) {
    var i;
    for(i=0;i=array1.length;i++) {
        if(array1[i]==array2[i]) {
            return false;
        }
    }
    return true;
}

or serialize the array in a comparable form ( comma separated string for example )

function compare_array(array1,array2) {
     return array1.join(",")==array2.join(",");
}
dvhh
Compare with serializing, nice! +1 :)
Nisse
not very reliable though, I would use it in every case
dvhh
A: 

Would be nice to see the HTML code. But I guess you want to do something like this:


var laArray = [];
var compareValues = function(arr1, arr2) {
  $(arr1).each(function(index, el) {
   if(el !== arr2[index]) {
     return false;
   }
  });
  return true;
};

$('.button-show-advice').click(function(){
    $(":radio:checked").each(function(i){
        laArray.push($(this).val());        
    });
   if(compareValues(laArray,["a","d","g","j","m","u"])) {
      $("#advice-container, #advice1, #advice2").show();
   }  
});

EDIT: updated the code, forgot the }); ...

Nisse
HTML (form) part 1:
tschardak
I appreciate your help very much, Nisse! However, this solution didn't seem to work. since I can't post the HTML here I am not sure what I am doing wrong...
tschardak
I forgot the missing }); in the end of the each-loop. Try to copy the example again.
Nisse
var a = [1,2,3];var compareValues = function(arr1, arr2) { $(arr1).each(function(index, el) { if(el !== arr2[index]) { return false; } }); return true;};compareValues(a, [1,2,3]); //true
Nisse
excellent! your solution works :)thank you very much, Nisse!hold on, now comes even more ignorant question...if i need to repeat the below step (changing just the values and the elements to be shown):if(compareValues(laArray,["a","d","g","j","m","u"])) { $("#advice-container, #advice1, #advice2").show(); } ... would that be a horrible thing to do?
tschardak
If you can't post your HTML here, then insert all of your code to http://jsfiddle.net and submit the link here.
Nisse
http://jsfiddle.net/tschardak/HQeXH/
tschardak
http://jsfiddle.net/HQeXH/2/
Nisse
thanks again! but, I apologize, because I don't understand what to do with the two alerts I have now. ...still wondering if i can repeat the below step form your first solution and compare to ["b","d","g","j","m","u"]... etc (all possible cobminations> if(compareValues(laArray,["a","d","g","j","m","u"])) { $("#advice1, #advice2").show(); }
tschardak
i think i got it all wrong, still trying to implement your script but keep on getting stuck. any suggestions? now i am trying to figure out if the idea of checking each value with in Array is better solution in this case...
tschardak