Solved

Select child element of the current element

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

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

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now