tags:

views:

31

answers:

2

I construct an array, which I am going to use later to compare some values. I use this code to construct "myArray", which I am going to use later:

var optionTexts = [];  
$('#stripeMeSubSubCat tr').each(function(){

 if ($(this).find('input:checkbox.subsubcat_chkclass:not(:checked)').length == 0)
 {
   subsubrow_cat_id_no = parseInt($(this).closest("tr").attr("id")); 

   optionTexts.push(subsubrow_cat_id_no);
  };   

 });

var myArray = optionTexts.join(', ');
alert("myArray = "+myArray);

Here I use myArray to make a comparison:

$('#stripeMeSubSubCat tr').each(function(){  

     myindex = $.trim($(this).closest("tr").attr("id"));
     var arr = [myArray];

     //var arr = [0, 439, 52, 53];

     myindex = parseInt(myindex);

     alert(myindex);
     alert(arr);

     if ((jQuery.inArray(myindex, arr)) == -1) {
       var equal = "FALSE";
     } else {
       var equal = "TRUE";
     $("#stripeMeSubSubCat tr[id='" + myindex + "'] input").attr('checked', true);
     };

     alert(equal);
 });

As is the code above it always returns FALSE. However, if don't use the first chunk of code and just enable the commented out var arr (where I set the array to fixed data) it returns TRUE where it should.

Any ideas?

A: 

myArray is a string, the result of

var myArray = optionTexts.join(', ');

Later, you try and change it back into an array with:

var arr = [myArray];

Which will actually create an array with the myArray string as the first and only element. Use split(", ") instead:

var arr = myArray.split(", ");
Andy E
I added alter what you have suggested and removed also myindex = parseInt(myindex); and it worked!Thank you for your prompt reply and response.
The most strange happens now though. When I remove the alerts from the code it doesn't change the attribute of the checkbox. How is that?
@andreas7: I don't actually see where you set the `myindex` property. Also, although this might not be the cause of the problem, `id` attributes [must start with a letter](http://www.w3.org/TR/html401/types.html#type-name) - they cannot be numerical only.
Andy E
+1  A: 

myArray is a string variable created by concatenating the elements of the optionsText array, not a set of elements. You should be checking optionsText instead of myArray.

if ((jQuery.inArray(myindex, optionsText)) == -1) {

As an aside, why set the variable equal to the string "FALSE" or "TRUE" -- why not simply set it to false or true (the boolean values)?

tvanfosson
with this way it also works if I keep myindex = parseInt(myindex);The most strange happens now though. When I remove the alerts from the code it doesn't change the attribute of the checkbox. How is that?
@andreas7 - I have no idea how the code you have above is working because `myArray` isn't one and thus `arr` is an array containing only the single element -- the value of `myarray`. Even if `myarray` were an array, `arr` would be an array containing a single element, which itself happens to be an array. I can see where you have to convert `myIndex` to a numeric value otherwise you're comparing strings to numbers, but I think you're getting spurious results and you really ought to be checking `optionsText` for the value.
tvanfosson
I have made the change as you proposed and thus not using myArray anywhere. It works fine as long as the alerts are within the code.If i remove them it doesn't work. Is there a synchronization issue, how can I manage it?
@andreas7 - is the whole thing executed after the DOM is ready using the `ready` method. `$(function() { ...your code here... });`
tvanfosson
my whole code is within $(document).ready(function() { HERE});
@andreas7 - see @AndyE's comment on his answer about numeric only ids being illegal in HTML. Perhaps this is your problem.
tvanfosson