display child elements and their text using jQuery

by Developer 17. January 2010 17:59

Getting the child elements of any HTMT element is very difficult in normal javascript. But it is very easy by using jQuery.

 

In the below code I explain how to display child elements by using jQuery.

 

<html>
  <head>
  <title>This is jquery example for displaying the number child elements and their text for given parent element</title>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
        var $nodes = $('#parent').children();
        alert('Number of Elements for given tag: ' + $nodes.length);
        var txt = "";
        $('#parent').children().each(function() {
            txt += $(this).text() + '\n';
        });
        alert('Text of all elements:\n\n' + txt);
        alert('This is "p" tag text:\n\n' + $('#pid').text());
    });   
  </script>
  </head>
  <body>
      <div id="parent">
           <div>First Element</div>
           <div>Second Element</div>
           <div>Third Element</div>
           <div>Fourth Element</div>
      </div>
      <p id="pid">This is from 'p' tag</p>
   </body>
</html>

Here children() jQuery function to get the childs of given parent element. By using $('#parent').children() code, we are getting the child elements of parent element whose id is "parent".

By using text() jQuery function we can get the text of any given element.

 

                               $(document).ready(function() {
                                                   var $nodes = $('#parent').children();
                                                  alert('Number of Elements for given tag: ' + $nodes.length);
                                                           var txt = "";
                                                  $('#parent').children().each(function() {
                                                                      txt += $(this).text() + '\n';
                                                                  });
                                                            alert('Text of all elements:\n\n' + txt);
                                                          alert('This is "p" tag text:\n\n' + $('#pid').text());
                                                    });   

 

In the above code we display the child elements of "parent" id parent element using children() jQuery function and their text by using text() jQuery function. At the end I also display the individual <p> tag text whose id is "pid".

 

Download source code: third jQuery exmp.zip (35.80 kb)

Comments

2/26/2010 8:29:28 AM #

Watch Anime Online

Wow this is an interesting article. I hope to see more in the future. While you're reading this you might as well check out http://www.lovelyanime.com for over 550 Anime series to watch online for FREE!

Watch Anime Online

3/2/2010 10:05:34 AM #

Daniel Millions

I wasn't sure where to post this but I would like to include your website in one of my web directories. Please contact me at your convience because I will only list webistes with the webmasters consent - thank you.

Daniel Millions

3/4/2010 10:32:52 PM #

hd movies

I love watching movies online, it is way cheaper than going to the theaters.

hd movies

3/10/2010 8:07:50 PM #

blog topics

Thank you very much for providing this post.

blog topics

3/12/2010 5:13:22 AM #

ELIZABETH PERKINS

hello good website yea nice work our blog will soon be adding reviews on websites and add them to our websites as the top best 25 websites to visit we also do reviews on product recalls  all types of reviews thanks

ELIZABETH PERKINS

3/23/2010 9:38:29 AM #

Train shirts

Thanks for writing about this. You've got a bunch of really good information here on your website.
Thanks mate.

Train shirts

4/5/2010 1:25:05 AM #

Stevie

Interesting blog, not like the others!

Stevie

4/19/2010 2:07:00 PM #

Colby

Interesting blog, not like the others!

Colby

4/30/2010 11:57:55 PM #

Ottawa Tutor

Definitely agree with what you stated. Your explanation was certainly the easiest to understand. I tell you, I usually get irked when folks discuss issues that they plainly do not know about. You managed to hit the nail right on the head and explained out everything without complication. Maybe, people can take a signal. Will likely be back to get more. Thanks

Ottawa Tutor

5/19/2010 1:44:26 PM #

ajit

Aghihhj

ajit

5/19/2010 2:37:19 PM #

ajit

jkkkkkkkkk

ajit

5/19/2010 8:35:17 PM #

the best opensource realestate application

Sweet... This is really what I'm scouting to find.... Thank you in spreading. Incase you need to find out about Digital Marketing campaigns you need to visit http://digitalmarketechs.com

the best opensource realestate application

5/28/2010 5:29:55 PM #

don't whack your boss

hey admin, your webpage's template is perfect and i like it. Your reports are marvelous. You should continue this awesome work. Cheers!!

don't whack your boss

7/8/2010 1:40:17 PM #

Top Interview questions

Perhaps you have thought about adding a few video clips to your site to keep the fans more entertained? I mean I just read thru the entire article of yours and it was ok but since most people are more of a visual learner, I found that to become more helpful. Well you should let me know how it turns out! I like what you people are doing and up too. Such interesting work and opinions! Keep up the great work guys. I've added this site to my page links. This is a great article thanks for sharing this informative information.. I will visit your site regularly for some latest post.

Top Interview questions

7/28/2010 7:28:04 AM #

ipad

It's the little things that matter, that's what I believe.

ipad

8/30/2010 12:47:46 AM #

tri cities roofing company

tri cities roofing company

Add comment




  Country flag

Click to change captcha
biuquote
  • Comment
  • Preview
Loading



Page List

    Tag cloud