Solved

Select child element of the current element

Posted on 2008-10-07
7
14,607 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 500 total points
ID: 22661917

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

Open in new window

0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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