jquerycsscode-injectionaddclass

jQuery addClass does not apply style dynamically


I'm trying to apply a style dynamically to a few dropdown lists in my page. Using jQuery addClass does indeed add the class to all 'Select' elements, as i can see this from my Chrome console. However the styles are not applied to the elements.

Now if i manually add the class to each select element individually, and then save the page and refresh it, the style is applied to the element.

Any idea why the styling is not applied with jquery class injection?

$("select").addClass("select js-select");

Solution

  • You said that the styles are not applied, but are the classes "select js-select" added to the select inputs? Then there must be some problem with the css or maybe the browser.

    $("select").addClass(..) should work, it isn't required that you specify by ID or class.