Solved

Loop over classes using jQuery

Posted on 2010-08-30
8
328 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
Comment Utility
What about :


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

Open in new window

0
 
LVL 1

Expert Comment

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

Expert Comment

by:leakim971
Comment Utility
miss something, the dot :


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

or

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility

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

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Author Comment

by:matthew016
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the points!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now