...

View Full Version : jQuery: Parent > Child .text()



OpenCode
02-11-2011, 09:55 AM
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:


<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.


Array
(
0 = > array (
0 => "David",
1 => "541"
),

1 = > array (
0 => "Gruce",
1 => "162"
),

2 = > array (
0 => "Proman",
1 => "743"
)
)


Thanks

Spudhead
02-11-2011, 11:36 AM
jQuery(".childOne").text(); will get the text of all elements with that class. Try using each() (http://api.jquery.com/each/) to loop through parent elements:


var ary = [];
$('.parent').each(function(){
ary[ary.length] = [$(this).find('.childOne').text(), $(this).find('.childTwo').text()];
});



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum