0

Below is my JS. When the page loads, I want it to automatically run the function as if its being clicked on. The default selection makes it started selected, but doesn't initiate the function. Any idea on how to make that work?

$(function() {
    $( "#selectable" ).selectable({
        selected: updatefilters  
    });   
    function updatefilters(ev, ui){
        // get the selected filters
        var template, html;
        var pagego;
        if(! pagego){
        var page = 0; 
        }
        var $selected = $('#selectable').children('.ui-selected');
        // create a string that has each filter separated by a pipe ("|")
        var filters = $selected.map(function(){return this.id;}).get().join("\|");
        $.ajax({
            type: "POST",
            url: 'updatefilters',
            dataType: 'json', 
            data: { filters: filters, page: page },
            success: function(data){
                html = "Do Something";
                $('#board').html(html); 
            }
        });
    }
    $('#lets').addClass('ui-selected') 
});

Thanks!

2 Answers 2

2

Just call the function after declaring it:

$(function() {
    $( "#selectable" ).selectable({
        selected: updatefilters  
    });   
    function updatefilters(ev, ui){
        ...
    }
    $('#lets').addClass('ui-selected');

    // none of your params are consumed in the function anyways, so you don't need to pass anything
    updatefilters(); 
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try to run function in the body tag

<body onload="name_of_function()">

1 Comment

there's no good reason to suggest inline javascript when he has access to $(document).ready();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.