For the selectpicker class, the usual assignment through .attr(attr_name, attr_value) does not work.

It works correctly through $('#myselect').selectpicker({attr_name: attr_value}) .

attr_name - unknown, and received as a variable:

 var attr_name = some_value; 

But! How now to use the VALUE of a variable as an attribute?

  • @webpauk, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

If I'm not mistaken, the only option is to create an object.

 var obj = {}; obj[attr_name] = some_value; $('#myselect').selectpicker(obj);