Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery: Find the matching class name

Posted on 2012-03-18
3
Medium Priority
?
357 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 500 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 83

Accepted Solution

by:
leakim971 earned 1000 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 43

Assisted Solution

by:David S.
David S. earned 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

824 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