views:

60

answers:

2

Here I construct an array:

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(parseInt(subsubrow_cat_id_no));
            };          
    });

The code below only works whenever the alert is enabled. I have read that this might be due to a synchronization issue. Is there a solution towards the code below? Thank you.

$('#stripeMeSubSubCat tr').each(function(){                                      
        myindex = parseInt($.trim($(this).closest("tr").attr("id")));

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

        //alert(equal);
});
A: 

You probably need to wait for the DOM to load. Try wrapping this code in:

$(function(){
  // put code here
});
nicholaides
both of the above are already included in that.
+1  A: 

What do you mean by "only works whenever the alert is enabled"? You need to describe the difference in behavior in both cases.

Your code has no asynchronous calls, so there shouldn't be synchronization issue. However, you seem to be using an integer as a DOM element id. This isn't allowed.

kgiannakakis
If I remove // from //alert(equal); in the code and thus enable the alert call, it works.
Yes, but what works mean? What happens if you include it and what if you don't?
kgiannakakis
If i include it then this $("#stripeMeSubSubCat tr[id='" + myindex + "'] input").attr('checked', true); is applied where appropriate, whereas if i don't, nothing is applied or maybe is applied before I am actually loading it. I will post the full js code of my file with detailed explanation tomorrow morning. Thank you for your prompt response.