Solved

jQuery: First class that ends with match

Posted on 2013-05-21
2
340 Views
Last Modified: 2013-05-22
Using JavaScript/jQuery, how can I get the first class item that ends with "aspx"?

<!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() {

 alert('The first class item in the body tag that ends with aspx is ' + 'exampleaspx');

});

</script>
<style type="text/css">
div {
width: 100px;
height: 100px;
margin: 50 auto;
border: 5px solid black;
}
</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 82

Assisted Solution

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

Open in new window

0
 
LVL 42

Accepted Solution

by:
David S. earned 350 total points
ID: 39185826
This should find it for you:
var m=$('body').attr("class").match(/[a-z-]+aspx(\b|$)/i);
var myClass = m?m[0]:'';

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now