views:

84

answers:

4

I have this code:

$(acc_id).accordion({
    alwaysOpen: false,
    active: false,
    autoheight: false,
    header: 'h3.ui-accordion3-header',
    clearStyle: true
});

I want to send a variable to it, since the header should be unique.

A: 

Try:

function Accord(acc_id, header_id) {
    $('#' + acc_id).accordion({
        alwaysOpen: false,
        active: false,
        autoheight: false,
        header: header_id,
        clearStyle: true
    });
}
rochal
The parameter name header_id is confusing because it can be any valid selector.
ChaosPandion
yes, good point. I like to mark stuff with _id when I need to pass string selector to distinguish between passing the entire object: header = $('.header') vs header_id = 'header'. I probably need better suffix/prefix for it.
rochal
+2  A: 
var header = 'h3.ui-accordion3-header';
$(acc_id).accordion({
        alwaysOpen: false,
        active: false,
        autoheight: false,
        header: header,
        clearStyle: true
    });
ChaosPandion
A: 

Not totally sure what you're looking for, but something like this may work

$(acc_id).accordion({
    alwaysOpen: false,
    active: false,
    autoheight: false,
    header: $(this).find('h3.ui-accordion3-header').text(),
    clearStyle: true
});
Jiaaro
This will probably raise an error internally. If you remove the .text() it should work. In either case this doesn't answer the question.
ChaosPandion
This way worked pretty well with me, thanks.
zeina
can I get the accepted answer? or at least an upvote? haha
Jiaaro
+1  A: 

The approach given in the examples by ChaosPandion and rochal (where the header is defined from a variable) seems best to me, but another useful thing to know about is that you can redefine any option on an accordion by using the "option" option, like this:

$(acc_id).accordion({
                                alwaysOpen: false,
                                active: false,
                                autoheight: false,
                                header: 'placeholder',
                                clearStyle: true
                        });
$(acc_id).accordion('option', 'header', 'h3.ui-accordion3-header');

In this way you can create the accordion and then, afterwards, set the header value.

JacobM