Hube02
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...
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...
$(this).each(function(){
alert($('p').get());
})
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
@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>
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........
Now I'm going to have more questions........
$('ul#RightSliderMenu').each(function(){
$(this).children('li').each(function(){
$(this).children('ul').each(function(){
alert(this);
});
});
});
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;
}
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;
}
http://www.w3schools.com/dom/prop_document_childnodes.asp