jQuery: how to i grab elements of a certain depth?

Given the following html:

<div class='module'>
   <div id='a' class='element'>
      <div class='element'></div>
      <div class='element'></div>
   <div id='b' class='element'></div>
   <div>
      <div id='c' class='element'></div>
   </div>
</div>

What should the jquery selector look like in order to grab the first descendants of class "element", but not any of the descendants which have the class element. To clarify, the selector should return the elements with id a,b,c as assigned above.

$('.module .element:XXXX'); ???
LVL 1
paddycobbettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
the markup you posted is not valid. You are not closing the <div id='a'>

If a, b, and c are at the same level immediately after module then use:
.module > .element.

otherwise you can try the filter method:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>hielo</title>
		<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
		<script>
		$(function(){

			$(".module .element").filter(function(){ return $(this).parent().attr('class')=='module' || ($(this).parent().parent().attr('class')=='module' && $(this).parent().attr('class')=='');}).each(function(){
				alert(this.id);
			})

		});
		</script>
	</head>
	<body>

<div id='module' class='module'>
	<div id='a' class='element'>
		<div id='a1' class='element'>a - child 1</div>
		<div id='a2' class='element'>a - child 2</div>
	</div>
	<div id='b' class='element'>b - child</div>
	<div>
		<div id='c' class='element'>c - child</div>
	</div>
</div>

	</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.