Solved

Select child element of the current element

Posted on 2008-10-07
7
14,603 Views
Last Modified: 2008-12-19
I'm trying to select the child element of the current element.

What I have:

"this" holds the current element which is a <li> element so if I do alert(this); what I get is:

[object HTMLLIElement]

What I want to get is The ul elements that are children of this li (not grandchildren so the normal selector would be li>ul)

How?

Hopefully this makes sense, if not let me know how I can clear it up...
0
Comment
Question by:Hube02
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:wktang83
ID: 22661865
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22661874

$(this).each(function(){

  alert($('p').get());

})

Open in new window

0
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22661917

alert($(this).children('p').get());

Open in new window

0
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.

 
LVL 18

Author Comment

by:Hube02
ID: 22662364
@wktang83:I should have been more specific, I am looking for a JQuery solution.

@sh0e: This seems to alert all decedent <ul>'s of the <li> and not just the chlidren.

so, for example I have this

<ul id="wft">
  <li>This is the LI that is in the variable "this"
    <ul>     <!-- I want to get this u lelement -->
      <li>I do no want to get the child ul elements of this li
        <ul><li>I should not get this</li></ul>
      </li>
    </ul>
  </li>
</ul>
     
0
 
LVL 18

Author Comment

by:Hube02
ID: 22662594
@sh0e: Missed your second comment,, this seems to work to get each of the li elements and the each child ul elements.

Now I'm going to have more questions........
	$('ul#RightSliderMenu').each(function(){

		$(this).children('li').each(function(){

			$(this).children('ul').each(function(){

				alert(this);

			});

		});

	});

Open in new window

0
 
LVL 5

Expert Comment

by:MMeijer
ID: 22662597
plain javascript function
function getChildLists(li)

{

	var ret = [];

	for(i in li.childNodes)

		if(li.childNodes[i].tagName.toLowerCase() == "ul")

			ret.push(li.childNodes[i]);

	return ret;

}

Open in new window

0
 
LVL 5

Expert Comment

by:MMeijer
ID: 22662620
tagName can be undefined for textnodes use the following instead :)
function getChildLists(li)

{

	var ret = [];

	for(i in li.childNodes)

		if(li.childNodes[i].tagName && li.childNodes[i].tagName.toLowerCase() == "ul")

			ret.push(li.childNodes[i]);

	return ret;

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 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

16 Experts available now in Live!

Get 1:1 Help Now