-3

I am having a <li> like this:

<li class="product type-product status-publish"></li>

I am adding a class with this code:

$("li").addClass("firstclass");

The output of it is like this:

<li class="product type-product status-publish firstclass"></li>

Is it possible to force the class to be in first of all the classes so the output is gonna be like:

<li class="firstclass product type-product status-publish"></li>

Hope you guys can help!

PelHout
  • 83
  • 1
  • 12
  • 8
    It makes absolutely no difference whatsoever what order your classes are listed in – j08691 Apr 04 '19 at 15:27
  • To add to the above, I presume you want to do this because the styling rules from `firstclass` are not being applied? If so, research rule specificity in CSS – Rory McCrossan Apr 04 '19 at 15:28
  • If this is related to css rule conflicts then you should make your css selectors more specific for these classes – charlietfl Apr 04 '19 at 15:31
  • Yes it does matter because I am using this: https://stackoverflow.com/questions/25367868/wrap-divs-with-same-classes-and-multiple-classes-in-jquery. So I want `temp` always to be `0` – PelHout Apr 04 '19 at 16:03

1 Answers1

-4

You can, but its tedious and probably wont make a practical difference. You'll have to disable all the other classes, add the first one, and then add the others back, e.g.

$("li").toggleClass("product");
$("li").toggleClass("type-product");
$("li").toggleClass("type-product");
$("li").toggleClass("status-publish");
$("li").addClass("firstclass");
$("li").toggleClass("product");
$("li").toggleClass("type-product");
$("li").toggleClass("type-product");
$("li").toggleClass("status-publish");
R Fonseca
  • 1
  • 1