I’m writing a plugin where I would need to “Bind” a parameter.
Sometimes I need the value of this.checked, but sometimes it’s $(this).val() what I need, and there are cornercases when I need to check other attributes.
I need an “input parameter” for the initialization which somehow determines what to check for the value.
I can do this with eval, but it’s evil.
I can do this with a switch case, but I’m not sure I can prepare for everything.
What is the best way to do this?
$.fn.formSwitch = function (options) {
var settings = $.extend({
visibleholder: '#visible_holder',
hiddenholder: '#hidden_holder',
reparseform: this.closest('form')
}, options);
this.change(function () {
switchFormPart(settings.visibleholder,
settings.hiddenholder,
********this.checked, *********** <- I would like this as a parameter
settings.reparseform
);
});
};
You can pass a string like
"checked"or"val"as parameter and then:If param is
"checked", it will do equivalent tothis.checkedIf param is
"val", it will do equivalent to$(this).val()And so on.
Look into bracket notation, conditional operator and in operator