16

what it does:

<p class="foo"></p>
$('p').addClass('bar');

output:

<p class="foo bar"></p>

what i need:

<p class="bar foo"></p>

So is it possible to add a class to an element to the first position?

edit (context): theres a split function on the last class right now. if another class is added its appended to the second split array as well.

ggzone
  • 3,661
  • 8
  • 36
  • 59

6 Answers6

17
 (function($) {

    jQuery.fn.extend({
        prependClass: function(newClasses) {
            return this.each(function() {
                var currentClasses = $(this).prop("class");
                $(this).removeClass(currentClasses).addClass(newClasses + " " + currentClasses);
            });
        }
    });

})(jQuery);
user3670510
  • 171
  • 1
  • 2
8
function prependClass(sel, strClass) {
    var $el = jQuery(sel);

    /* prepend class */
    var classes = $el.attr('class');
    classes = strClass +' ' +classes;
    $el.attr('class', classes);
}
algorhythm
  • 8,530
  • 3
  • 35
  • 47
5

Here's another way to do it.

$('#foo').attr('class', 'new-class ' + $('#foo').attr('class'));
J. Minjire
  • 1,003
  • 11
  • 22
  • This is the best option as it takes the original class list and adds your necessary, new class name then adds it at the beginning. Nice work, John! – Jester Jun 27 '19 at 17:26
3

This should not be a dependency for you in any way at all because of the limited control you have over the className string, but you can do this:

$("p").removeClass('foo').addClass('bar foo');
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You can try this, live demo:

http://jsfiddle.net/oscarj24/eZe4b/

$('p').prop('class', 'bar foo');

Otherwise you will need to play around with addClass and removeClass to get bar foo result in that order.


By the other hand, I don't get at all your question but if you just want to add bar foo class to the first p element just do this:

$('p').eq(0).prop('class', 'bar foo');

Oscar Jara
  • 14,129
  • 10
  • 62
  • 94
  • 1
    what is if you add further classes to the orig. DOM-element. Then they will be deleted... – algorhythm Jan 22 '13 at 15:30
  • @algorhythm you can try `$(..).prop('class', 'firstClazz')` and just concat your next class to get `$(..).prop('class', 'secondClazz firstClazz')` and if there is other one then `$(..).prop('class', 'thirdClazz secondClazz firstClazz')` what matters is the string that you pass as the second argument in the `.prop()` method – Oscar Jara Jan 22 '13 at 15:32
  • 1
    Right, and then you have my solution ;) – algorhythm Jan 22 '13 at 15:36
0

Here's a no-hack solution for this:

$(".product").each(function(i, el) {
  var cList = [];

  $.each(this.classList, function(index, val) {
    cList.push(val);
  })

  this.classList.remove(...cList);

  cList.unshift("first-class");

  this.classList.add(...cList);

  // here it is
  console.log(this.classList);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li class="product type-product status-publish">a</li>
  <li class="product type-product status-publish">b</li>
  <li class="product type-product status-publish">c</li>
</ul>
muka.gergely
  • 8,063
  • 2
  • 17
  • 34