Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select child element of the current element

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

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

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

885 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