Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Select child element of the current element

Posted on 2008-10-07
7
Medium Priority
?
14,614 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
[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
  • 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 2000 total points
ID: 22661917

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

Open in new window

0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
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

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

718 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