Solved

Loop over classes using jQuery

Posted on 2010-08-30
8
332 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
[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
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…
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…

740 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