Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2011, 09:55 AM   PM User | #1
OpenCode
New to the CF scene

 
Join Date: Jan 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
OpenCode is an unknown quantity at this point
Question jQuery: Parent > Child .text()

Hello,

I'm having trouble to get the proper formatted texts of each child elements, either as an Array or in as text.

I had tried

var name= jQuery(".childOne").text();
var number = jQuery(".childTwo").text();

but it joins all the name/number text in name and number.

HTML is:
Code:
<span class="parent"><span class="childOne">David</span><span class="childTwo">541</span></span>
<span class="parent"><span class="childOne">Gruce</span><span class="childTwo">162</span></span>
<span class="parent"><span class="childOne">Proman</span><span class="childTwo">743</span></span>
and I need to generate output in multi-dim-array so that each child-element's-text can be figured out properly.

Preferred output can be in array or in any form.
Code:
Array
(
    0 = > array (
            0 => "David",
            1 => "541"
        ),
        
    1 = > array (
            0 => "Gruce",
            1 => "162"
        ),
        
    2 = > array (
            0 => "Proman",
            1 => "743"
        )
)
Thanks
OpenCode is offline   Reply With Quote
Old 02-11-2011, 11:36 AM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
jQuery(".childOne").text(); will get the text of all elements with that class. Try using each() to loop through parent elements:

Code:
var ary = [];
$('.parent').each(function(){
	ary[ary.length] = [$(this).find('.childOne').text(), $(this).find('.childTwo').text()];
});
Spudhead is offline   Reply With Quote
Users who have thanked Spudhead for this post:
OpenCode (02-11-2011)
Reply

Bookmarks

Tags
jquery, parent > child elements

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:05 AM.


Advertisement
Log in to turn off these ads.