for example I have the following structure:
<div class="parent">
<div>1st child</div>
<div>2nd child</div>
<div>3rd child</div>
<div>4th child</div>
</div>
all I want is to have the text of all the children of the parent div as items of an array.
the jQuery solution is preferred.
var a = $(".parent").children().map(function() { return $(this).text(); });
function get_this_text() {
return $(this).text();
}
$(".parent div").map(get_this_text).get()
Here's it on fiddle (see result in your console): http://jsfiddle.net/C6pHS/
Optionally to the other answers could also do this if it helps you to see things more clearly. HTML:
<div class="parent">
<div class="child">1st child</div>
<div class="child">2nd child</div>
<div class="child">3rd child</div>
<div class="child">4th child</div>
</div>
JQuery:
var childArray = $('.child');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.