Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery: Elements with class name beginning with term

Posted on 2012-03-16
4
Medium Priority
?
221 Views
Last Modified: 2012-03-18
Using jQuery, how can I select all elements that have a class name beginning with "hello"?

<!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">

$(document).ready(function() {
 $('.hello*').each(function(index) {	
  alert($(this).attr('class'));
 });
});

</script>
</head>
<body>

<h1 class="helloH">Heading</h1>
<div class="hello1">1</div>
<div class="hello2">2</div>
<div class="hello3">3</div>
<p class="nomatch">This should not be included</p>
<p class="helloP">ABC</p>
<p class="helloXYZ">XYZ</p>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 37730646
$('[className^="hello"]').each(function(
0
 
LVL 16

Author Comment

by:hankknight
ID: 37730802
Thanks, but it does not work for me.  Please test this:

<!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">

$(document).ready(function() {
 $('[className^="hello"]').each(function(index) {
  alert($(this).attr('class'));
 });
});

</script>
</head>
<body>

<h1 class="helloH">Heading</h1>
<div class="hello1">1</div>
<div class="hello2">2</div>
<div class="hello3">3</div>
<p class="nomatch">This should not be included</p>
<p class="helloP">ABC</p>
<p class="helloXYZ">XYZ</p>

</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 37731436
Sorry for the delay I was very busy...
replace className by class :

So use : $('[class^="hello"]')
or : $('*[class^="hello"]')
Test page :
<!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">

$(document).ready(function() {
 $('[class^="hello"]').each(function(index) {
  alert($(this).attr('class'));
 });
});

</script>
</head>
<body>

<h1 class="helloH">Heading</h1>
<div class="hello1">1</div>
<div class="hello2">2</div>
<div class="hello3">3</div>
<p class="nomatch">This should not be included</p>
<p class="helloP">ABC</p>
<p class="helloXYZ">XYZ</p>

</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 37731437
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…

618 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