Solved

jQuery: Find the matching class name

Posted on 2012-03-18
3
317 Views
Last Modified: 2012-03-18
Hello,

How can I find the class name that matches?  In the example below, I get additional class names.  I only want the alert to contain class names that have "hello" in them.

Instead of:
adsfd helloABC xyz adfdsfdasfadsf

The alert should be:
helloABC

<!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">
// Thanks, leakim971
$(document).ready(function() {
 $('.xyz[class*="hello"]').each(function(index) {
  alert('The matching class is: ' + $(this).attr('class'));
 });
});

</script>
</head>
<body>

<h1 class="adsfd helloABC xyz adfdsfdasfadsf">Heading</h1>
<div class="hello1">1</div>
<div class="adsfdsf hello123 dasfads fdsa sd adsf xyz">2</div>
<div class="hello3">3</div>
<p class="nomatch  xyz">This should not be included</p>
<p class="helloP">ABC</p>
<p class="helloTest cvsdfadfasf xyz">XYZ</p>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 125 total points
ID: 37735273
you can check inside if the class name contains a space or not

 $('.xyz[class*="hello"]').each(function(index) {
  if ( $(this).getAttr("class").indexOf(" ") != -1 )
  {
      alert('The matching class is: ' + $(this).attr('class'));
  }
 });
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 37735291
Test page : http://jsfiddle.net/NPGCs/2/
<!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">
// You're welcome!
$(document).ready(function() {
 $('.xyz[class*="hello"]').each(function(index) {
  alert('The matching class is: ' + $(this).attr('class').match("\s*hello[^ ]*")[0] );
 });
});¿

</script>
</head>
<body>

<h1 class="adsfd helloABC xyz adfdsfdasfadsf">Heading</h1>
<div class="hello1">1</div>
<div class="adsfdsf hello123 dasfads fdsa sd adsf xyz">2</div>
<div class="hello3">3</div>
<p class="nomatch  xyz">This should not be included</p>
<p class="helloP">ABC</p>
<p class="helloTest cvsdfadfasf xyz">XYZ</p>

</body>
</html>

Open in new window

0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 125 total points
ID: 37735293
This should only show the classes that start with "hello":
$(document).ready(function() {
  var helloRE = /(^|\s)hello\S*/ig;
  $('.xyz[class*="hello"]').each(function(index) {
    var myClass=$(this).prop('class').match(helloRE);
    alert('The matching class is: ' + (myClass!=null?myClass.join(' '):''));
  });
});

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

685 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