Alan2
Alan2

Reputation: 24572

How can I replace a class name using jQuery when I just know the start characters of the name?

I have:

<tr id='id1' class='X-** other-class a-class'> 

Where the * can be any number.

If I want to replace the class name that starts with X-99 then how can I do this?

Upvotes: 1

Views: 55

Answers (3)

Ram
Ram

Reputation: 144689

try this:

if ($('#id1').get().className) {
    $('#id1').get().className = $('#id1').get().className.replace(/\bX\-.*?\b/g, 'X-99');
}  else {
    $('#id1').addClass('X-99')
}

Upvotes: 1

Zbigniew
Zbigniew

Reputation: 27594

Try those selectors:

// all trs with X-* class (that is: classes which starts with X-)
$("tr[class^='X-']") 

// all trs with X-99 class
$("tr.X-99") 

Then you can use addClass or removeClass to set or unset classes.

Upvotes: 0

Kris Khairallah
Kris Khairallah

Reputation: 1582

jQuery(".X-99").removeClass(".X-99"); //class level
jQuery("#id1").removeClass(".X-99"); //id level

what do you want to do exactly ?

Upvotes: 0

Related Questions