?
Solved

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

Posted on 2010-03-31
1
Medium Priority
?
508 Views
Last Modified: 2012-05-09
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'); ???
0
Comment
Question by:paddycobbett
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 29282222
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

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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…
Suggested Courses

593 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