An element will not be added if it has a specific class

So, I'm trying to add a select, and it works great when that plugin's class, bootstrap-select, is not connected:

var select = $("<select class='new-select'></select>");
$('#selects-container').append(select);

      

However, when I add the "selectpicker" class, the selection is added, but it is not processed through the plugin javascript and therefore not displayed.

var select = $("<select class='new-select selectpicker'></select>");
$('#selects-container').append(select);

      

So my question is how to get the new added selection to be run through the plugin's javascript and formatted the same as the selection that was present when the screen was loaded.

Sincere thanks for your help, it is greatly appreciated.

+3


source to share


1 answer


Thus, there are several ways to declare new DOM nodes using jQuery. For a specific task, you want to supply the options object as the second argument and then add attributes to it, rather than in the selection string itself.

Try the following:

var select = $('<select></select>', {
  'class': 'new-select selectpicker'
});
$('#selects-container').append(select);

      



Also, notice this: docs :

The name "class" must be specified in the object because it is a JavaScript reserved word and "className" cannot be used because it refers to the DOM and not an attribute.

+1


source







All Articles