Question

I know it's been asked like a million times, but everything I found on web not working for me so maybe it's not that. I'm trying to populate subcategories array, dynamically based on current category and then use that array as a source for x-editable edit. Problem is that every time I change category, subcategories array is updated with new categories but old subcategories still remain in there. I tried setting up length property to 0 before ajax but no joy.

var subcategories = [];

    $('body').on('hover', '.subcategory_editable', function(){

        $.ajax({
            type:"post",
            dataType: "json",
            data: "category="+$(this).attr('category_id'),
            url:"ajax_php/account_page/get_all_subcategories.php",
            success:function(data){
                $.each(data, function(k, v ) {
                        subcategories[v.id] = v.subcategory
                });
            }
         });
    });

    $(document).ajaxComplete(function() {
        $(".subcategory_editable").editable({
            inputclass:'not_select',
            value:'',
            source:subcategories
        });
    });
Was it helpful?

Solution

Just reset subcategories each time the AJAX completes:

success:function(data){
    subcategories = [];
    $.each(data, function(k, v ) {
        subcategories[v.id] = v.subcategory
    });
}

OTHER TIPS

 var subcategories = [];

    $('body').on('hover', '.subcategory_editable', function(){

        $.ajax({
            type:"post",
            dataType: "json",
            data: "category="+$(this).attr('category_id'),
            url:"ajax_php/account_page/get_all_subcategories.php",
            success:function(data){
                subcategories = []
                $.each(data, function(k, v ) {
                        subcategories[v.id] = v.subcategory
                });
            }
         });
    });

    $(document).ajaxComplete(function() {
        $(".subcategory_editable").editable({
            inputclass:'not_select',
            value:'',
            source:subcategories
        });
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top