Lakshman Pilaka
Lakshman Pilaka

Reputation: 1951

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.

Upvotes: 1

Views: 147

Answers (2)

Lakshman Pilaka
Lakshman Pilaka

Reputation: 1951

with the help of @charlietfl i could solve with the following code

function showPanel(panelClassName){
var panelOpts = {
    "state": "unpinned", 
    "resize": "both",
    "reload": false,
    "unpin": false,
    "editTitle": false
}; 

$(function(){
    $(panelClassName).lobiPanel(panelOpts);
    });
}

showPanel('.lobipanel-basic');

@charlietfl: i wanted to load a panel on click of a button. now i can assign the showPanel function on button click.

thanks

Upvotes: 0

charlietfl
charlietfl

Reputation: 171669

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);
});

Upvotes: 1

Related Questions