0

What jquery can I use to change the class that's used in the following from indent_1 to indent_2 or indent_4 to indent_2? I know about remove class but how can I do that when the classes are names that vary?

<div class="rep_td0 indent_1" id="title_1">Menu two</div> 
or 
<div class="rep_td0 indent_4" id="title_1">Menu two</div>
Eli
  • 14,779
  • 5
  • 59
  • 77
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

5 Answers5

3

try this code,

$("#title_1").removeClass("indent_1").addClass("indent_2");

if you not sure which is available, try this

$("#title_1").removeClass("indent_1").removeClass("indent_4").addClass("indent_2");

Updated:

$("#title_1").removeClass(function() {
    var match = $(this).attr('class').match(/\bindent_\d+\b/);
    if (match) {
        return (match[0]);
    } else {
        return ("");
    }
}).addClass("indent_2");
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • I think this would work okay but is there a way to do it without specifying removal of all the classes. I want to change the indent level but I am not sure which level is set. If there were a lot of combinations it would mean hard coding each of them. – Samantha J T Star Jan 08 '12 at 06:57
3

Since you haven't been very specific about exactly what class you want to change to another and you've said you want to deal with the case where you don't know exactly what the class is, here are some ideas:

You can find all objects that have a class that starts with "indent_" with this selector:

$('[className^="indent_"]')

If you wanted to examine the class on each one of those objects, you could iterate over that jQuery object with .each() and decide what to do with each object or you could use removeClass() with a custom function and examine the class name and decide what to do with it.

If you just wanted to change all indent class names to indent_2, then you could use this:

$('[className^="indent_"]').removeClass("indent_1 indent_3 indent_4").addClass("indent_2");

or, using a custom function that can examine the class name with a regex:

$('[className^="indent_"]').removeClass(function(index, name) {
    var match = name.match(/\bindent_\d+\b/);
    if (match) {
        return(match[0]);
    } else {
        return("");
    }
}).addClass("indent_2");

Or, if all you want to do is find the object with id="title_1" and fix it's classname, you can do so like this:

var o = document.getElementById("title_1");
o.className = o.className.replace(/\bindent_\d+\b/, "indent_2");

You can see this last one work here: http://jsfiddle.net/jfriend00/tF8Lw/

If you're trying to make this into a function that could take different numbers, you could use this:

function changeIndentClass(id, indentNum) {
    var item = document.getElementById(id);
    item.className = item.className(/\bindent_\d+\b/, "indent_" + indentNum);
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks. how can I combine this with a check that the object has an id of title_1 ? – Samantha J T Star Jan 08 '12 at 06:54
  • Maybe I could do this: $('#title_1').removeClass("indent_1 indent_3 indent_4").addClass("indent_2"); – Samantha J T Star Jan 08 '12 at 07:00
  • @Melissa - I added an option at the very end of my answer that changes just the id="title_1" object. If you'd explained that that was all you wanted to do from the beginning, this is a way, way easier problem. – jfriend00 Jan 08 '12 at 07:07
  • Sorry for not explaining well. I tried the following: document.getElementById("title_" + id).className.replace(/\brep_td0 indent_\d+\b/, "rep_td0 indent_" + val); But actually it doesn't seem to work. Not sure what is wrong. I debug and see it get executed with all the correct values but the class names don't change. – Samantha J T Star Jan 08 '12 at 07:30
  • @Melissa - fixed the last option so it works now. There's also a jsFiddle that shows it working. – jfriend00 Jan 08 '12 at 07:30
  • @Melissa - also added a function version that takes the id and indent number as arguments. – jfriend00 Jan 08 '12 at 07:39
1

Try below :

$('#title_1').removeClass('indent_1').addClass('indent_2');

Here, the indent_1 classes will be replaced with indent_2.

  • Sorry but maybe I did not explain well. I cannot be sure the exact name of the indent class. The number after the _ can be different. I need a wildcard way of checking for a class that lookes like "indent_x" and then removing that class. – Samantha J T Star Jan 08 '12 at 06:53
0

Maybe this?: Remove all classes that begin with a certain string

That answers how to replace a classname on a jQuery element that has a specific prefix, such as 'indent_'.

While that answer doesn't specifically address replacement, you can achieve that by altering one of their answers slightly:

$("selector").className = $("selector").className.replace(/\bindent.*?\b/g, 'indent_2');

Or similar...

Community
  • 1
  • 1
digitlworld
  • 1,046
  • 7
  • 13
  • Nice find but (as I was informed a while back) if you know something is a duplicate it's better to post a comment on the question and say: Possible duplicate of ...insert link here... But thanks for finding that answer. – kasdega Jan 08 '12 at 06:58
  • @kasdega Fair enough, will do in the future. Wasn't a perfect dupe tho, so I just went with it. – digitlworld Jan 08 '12 at 06:58
  • Your link may have the answer. Something like: $('#title_1').className = $('#title_1').className.replace(/\bbg.*?\b/g, ''); except I am not sure how to code the regular expression. – Samantha J T Star Jan 08 '12 at 07:03
  • @Digitlworld - Thanks for your answer. It looks perfect. I checked out the link and while reading that you updated your answer. – Samantha J T Star Jan 08 '12 at 07:05
  • @Melissa RegEx is a programmer's best friend. Learn it. Love it. [Here!](http://www.regular-expressions.info/) – digitlworld Jan 08 '12 at 07:06
0

First things first...If you have multiple elements on your page with the exact same ID you're going to have problems selecting them by ID. Some browsers won't work at all while others may return just the first element that matches.

So you'll have to clean up the ID thing first.

You can use the starts with selector to find all the classes that match your class name patter and then decide if you want to switch them or not:

$('[class^="indent_"]').each(function() {
   var me = $(this);
   if(me.hasClass("indent_1").removeClass("indent_1").addClass("indent_2");
});
kasdega
  • 18,396
  • 12
  • 45
  • 89
  • I have multiple rows but the elements on each row are marked with a different title. So there would be the first row with title_1 and the second row with title_2 etc. the row number is known to me so I could pick out that row by doing a check for an element with that id. – Samantha J T Star Jan 08 '12 at 06:58