Solved

jQuery: Remove all classes that end with match

Posted on 2013-05-21
2
632 Views
Last Modified: 2013-05-22
Using jQuery, how can I remove ALL classes from the body tag that end with "aspx"?

This does NOT work:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

 $('body').removeClass("*aspx");

});

</script>
<style type="text/css">
div {
width: 100px;
height: 100px;
margin: 50 auto;
border: 5px solid black;
}
.exampleaspx div {
border: 5px solid red;
}
</style>

</head>
<body class="hello world exampleaspx test sampleaspx">

<div>
</div>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 250 total points
ID: 39185511
This should do it:
$(document).ready(function() {
	var classAr=document.body.className.split(/\s+/),re=/aspx$/i;
	$.each(classAr,function(i,val){
		if(re.test(val)) $('body').removeClass(val);
	});
});

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 39185526
var currClass = $(body).attr("class");
var newClass = currClass.replace(/\s[^\s]*aspx|^[^\s]*aspx\s+/g,"");
//alert(newClass);
$('body').attr("class", newClass);

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now