Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

jQuery: Find the matching class name

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

SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial