Solved

jQuery: Find the matching class name

Posted on 2012-03-18
3
304 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

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now