Link to home
Start Free TrialLog in
Avatar of Hube02
Hube02Flag for United States of America

asked on

Select child element of the current element

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...
Avatar of wktang83
wktang83


$(this).each(function(){
  alert($('p').get());
})

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sh0e
sh0e

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hube02

ASKER

@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>
     
Avatar of Hube02

ASKER

@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

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

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