0

i need to add class in JS or JQ to all the tables without class in a site, some already as class so i need it to add the class only to the tables who doesn't have... please advice? edit: i also need the class to be unique with number after the name of the class

$('table').each(function(i){
                $(this).addClass('classname',i);
            });
benjo
  • 351
  • 1
  • 5
  • 11
  • 1
    `addClass()` won't do anything if the class you want to add is already exposed by the element -- there is no need to handle that case yourself. – Frédéric Hamidi Oct 06 '16 at 13:50
  • 1
    Do you just want all your tables to get this new class, or do you specifically need to select only tables without any classes? – j08691 Oct 06 '16 at 13:54
  • There's no clear problem in the question unless you were trying to add `i` as a class but only to those that do not have `classname`. –  Oct 06 '16 at 13:54
  • I believe what you are looking for is the same as this post describing how to find an element without classes (just for the table element instead of a div): http://stackoverflow.com/questions/1962247/jquery-get-all-divs-which-do-not-have-class-attribute – kgiff Oct 06 '16 at 13:56
  • @BarryMichaelDoyle: You changed the wording of this question without first asking the OP if that's what was intended. –  Oct 06 '16 at 14:00
  • the class i need to add needs to be numbered so it will be unique, that is why i tried to usr "i" – benjo Oct 10 '16 at 08:15

4 Answers4

1

the addClass method only adds a class if it not exist in the selected element.

this works

$('table').addClass('classname');
Higarian
  • 533
  • 5
  • 11
1

If you need to add a class to only those tables without any classes (and not just all your tables), you can use:

$('table').each(function() {
  if (!$(this).attr('class') || !$(this).attr('class').length) $(this).addClass('zzz')
})
j08691
  • 204,283
  • 31
  • 260
  • 272
  • `attr('class')` will be undefined if no attribute exists .. will throw error trying to get length – charlietfl Oct 06 '16 at 14:20
  • @j08691 hello, this function almost done what i intended i also needs it to do unique number to the class, this is what i tried: `$('table').each(function(i) { if (!$(this).attr('class') || !$(this).attr('class').length) $(this).addClass('oldtableclassname',i) }); ` – benjo Oct 10 '16 at 08:37
0

You can check with not selector if particular class is not applied then apply it:

$("table:not(.classname)").addClass("classname");

Update:

But as others have mentioned you don't need to do that as addClass will take care of not adding class if it is there.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
-1

If you just want to target tables that don't have any class, you can use :not and the attribute selector

$('table:not([class!=""])').addClass('test');

that would be the same as:

$('table[class=""],table:not([class])').addClass('test');
Borgtex
  • 3,235
  • 1
  • 19
  • 28
  • Why the downvote? Note that the question has been edited and at the time of this answer it required targeting tables without classes (in plural) – Borgtex Oct 07 '16 at 11:22
  • hello, this function almost done what i intended i also needs it to do unique number to the class, this is what i tried: `$('table[class=""],table:not([class])').each(function(i) { $(this).addClass('nameofclass',i); });` please advice ? – benjo Oct 10 '16 at 08:59
  • You almost got it. Try with addClass('nameofclass'+i), so you will get nameofclass0,nameofclass1, etc. – Borgtex Oct 10 '16 at 09:14