views:

44

answers:

1

I have a link with a series of checkboxes, due to the legacy code, when the user clicks on a checkbox the value of it is appended as a comma deliminated list to the href.

The trouble is that now that I'm updating the code, I'm finding that the href is being followed quicker than the href is being adjusted, thus the list is being excluded.

I have tried using preventDefault() which is great, but I have no idea how to continue the default action after changing the href to include the selected values.

I should also mention that it is not viable to change it to a regular form, as there is an additional option set in a lightbox after submission. (Don't ask me why, it's lunacy in my book)

So far I've got to

$(function(){
    $('#cnt-area form table tbody').dragCheck();

    // I just split up the .each and .click to see if it mattered, which it doesnt
    $('.multi_assign_link').each(function(){
        $(this).click(function(e){

            e.preventDefault();
            var href = $(this).attr('href');

            $('#cnt-area form input:checkbox').each(function(){
                if($(this).attr('checked')){
                    if(href.search(','+$(this).val()) == -1){
                        href += ','+$(this).val();
                    }
                }else{
                    var s = ','+$(this).val();
                    s.toString();
                    href = href.replace(s, '');
                }
            });
            $(this).attr('href',href);
            // Continue to follow the link
            // Faking a user click here with $(this).click(); obviously throws a loop!
        });
    });
});
+3  A: 

Just set the location to the adjusted href value and return false from the handler to avoid taking the original link.

$(function(){ 
    $('#cnt-area form table tbody').dragCheck(); 

    // I just split up the .each and .click to see if it mattered, which it doesnt 
    $('.multi_assign_link').each(function(){ 
        $(this).click(function(e){ 

            var href = $(this).attr('href'); 

            $('#cnt-area form input:checkbox').each(function(){ 
                if($(this).attr('checked')){ 
                    if(href.search(','+$(this).val()) == -1){ 
                        href += ','+$(this).val(); 
                    } 
                }else{ 
                    var s = ','+$(this).val(); 
                    s.toString(); 
                    href = href.replace(s, ''); 
                } 
            }); 
            location.href = href;
            return false;
        }); 
    }); 
});
tvanfosson
I cannot use this, as I need to open a thickbox after the link has been clicked.
DavidYell
On reflection, Im going to get rid of that annoying thickbox all together!
DavidYell