Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  159] [ 7]  / answers: 1 / hits: 63186  / 14 Years ago, fri, november 5, 2010, 12:00:00

I'm currently using this to get the class for a specific bit of HTML on the page:



$(this).parent(div).attr('class')


But that div has multiple classes: current_status status_billed



My end goal here is to grab the class that starts with status_ and replace it with a different class name.



So using my .parent() function above, I'm able to select the div I need, but I then need to remove the status_billed class and replace it with, for example, status_completed (or a number of other class names).


More From » jquery

 Answers
20

Select divs that have the status_billed class:



$(this).parent('div.status_billed')


Select divs whose class attribute contains status_:



$(this).parent('div[class*=status_]')


That's about the best you'll get with jQuery selectors. You can do better using .filter():



$(this).parent('div').filter(function ()
{
var classes = $(this).attr('class').split(' ');
for (var i=0; i<classes.length; i++)
{
if (classes[i].slice(0,7) === 'status_')
{
return true;
}
}
return false;
});





...but I'm not sure why you're doing all this - .parent() returns at most 1 element. Did you mean .closest() or .parents()?


[#95064] Wednesday, November 3, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
christianu

Total Points: 481
Total Questions: 124
Total Answers: 99

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
1 Year ago
;