Lakshman Pilaka Lakshman Pilaka - 5 months ago 31
jQuery Question

Sending parameter to anonymous function

My code looks like this:

var x = $(function(){
$('.lobipanel-basic').lobiPanel({
"state": "unpinned",
"resize": "both",
"reload": false,
"unpin": false,
"editTitle": false
});
});

x();


This works just fine. What if I want to send a parameter to function
x()
. Basically I don't want to, for obvious reasons, hardcode the class name
.lobipanel-basic
.

Thanks.

Answer

If all you want is a function that can initialize a plugin by passing in a selector just do:

function initPanel(selector){
    $(selector).lobiPanel({
        "state": "unpinned", 
        "resize": "both",
        "reload": false,
        "unpin": false,
        "editTitle": false
    });
}

$(function(){
    initPanel('.lobipanel-basic');
});

Or store options in an object and do something like:

var panelOpts = {
    "state": "unpinned", 
    "resize": "both",
    "reload": false,
    "unpin": false,
    "editTitle": false
}; 

$(function(){
    $('.lobipanel-basic').lobiPanel(panelOpts);
});
Comments