Solved

Loop over classes using jQuery

Posted on 2010-08-30
8
330 Views
Last Modified: 2012-05-10
Hi,

I would like to loop of an element's classes.
I tried the following :

var clazz = $(this).attr('class');
$(clazz).each(function() {
   ...
});

Didn't work,
Thanks for any help
0
Comment
Question by:matthew016
  • 6
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33557426
What about :


var clazz = $(this).attr('className');
$(clazz).each(function() {
   ...
});

Open in new window

0
 
LVL 1

Expert Comment

by:revteam
ID: 33557432
could you please give more information of the elements.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33557450
miss something, the dot :


var clazz = $(this).attr('class');
$("." + clazz).each(function() {
   ...
});

or

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 82

Expert Comment

by:leakim971
ID: 33557453

var clazz = $(this).attr('className');
$("." + clazz).each(function() {
   ...
});

Open in new window

0
 
LVL 9

Author Comment

by:matthew016
ID: 33557611
There is no answer.

<div class="hello world">

How to loop over the classes ?
(first iteration, "hello", second "world").

Thanks
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33557683
Use split : http://www.w3schools.com/jsref/jsref_split.asp
So : $(this).attr('className').split(" ");
to get an array of classes

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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$(".Hello").click(function() {
			var clazz = $(this).attr('className').split(" ");
			for(var i=0;i<clazz.length;i++) {
				alert("doing the job for class : " + clazz[i]);
				$("." + clazz[i]).each(function() {
					// do what you want
				});
			}
		});
	});
</script>
</head>
<body>
<input type="button" class="Hello World" value="Hllo World" />
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33557911
Maybe you prefer something like this :


                  var clazz = $(this).attr('className').replace(/\s/g,",.");
                  $("." + clazz).each(function() {
                              // do what you want
                  });

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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$(".Hello").click(function() {
			var clazz = $(this).attr('className').replace(/\s/g,",.");
			$("." + clazz).each(function() {
				alert("doing the job for : " + $(this).attr("id") );
			});
		});
	});
</script>
</head>
<body>
<input type="button" class="Hello World" value="Hllo World" id="me" />
<input type="text" class="Hello" id="Hello" />
<input type="text" class="World" id="World" />
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 33566495
Thanks for the points!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
The viewer will learn how to dynamically set the form action using jQuery.
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…

816 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

10 Experts available now in Live!

Get 1:1 Help Now