Solved

jQuery: Find the matching class name

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

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

13 Experts available now in Live!

Get 1:1 Help Now